Form validation

rule

model-form uses lavel’s validation rules to validate data submitted by the form

  1. $form->text('title')->rules('required|min:3');
  2. // Complex validation rules can be implemented inside callbacks
  3. $form->text('title')->rules(function (Form $form) {
  4. // Add field unique validation if not edit status
  5. if (!$id = $form->model()->id) {
  6. return 'unique:users,email_address';
  7. }
  8. });

Custom error messages can also be given for validation rules:

  1. $form->text('code')->rules('required|regex:/^\d+$/|min:10', [
  2. 'regex' => 'The code must be all numeric',
  3. 'min' => 'Code cannot be less than 10 characters',
  4. ]);

If you want to allow the field to be empty, first set the field to NULL in the database table, then

  1. $form->text('title')->rules('nullable');

See Validation for more rules.

creationRules

This method is exactly the same as Form\Field::rule, except that it only works when data is added.

{tip} If the creationRules method is called, the validation rules set by the rule method will be ignored.

updateRules

This method is exactly the same as Form\Field::rule, except that it only works when you update the data.

{tip} If the updateRules method is called, the validation rules set by the rule method will be ignored.

responseValidationMessages

Custom validation error messages can be returned and subsequent logic interrupted by the Form::responseValidationMessages method, used as follows:

  1. // "PUT" method for editing submissions
  2. if (request()->getMethod() == 'PUT') {
  3. if (...) { // Your validation logic
  4. $form->responseValidationMessages('title', 'Wrong title format');
  5. // If there are multiple error messages, the second parameter can be passed as an array.
  6. $form->responseValidationMessages('content', ['content format error', 'content cannot be empty']);
  7. }
  8. }
  9. $form->text('title');
  10. $form->text('content');

You can also use this method in the submitted event

  1. $form->submitted(function ($form) {
  2. if (...) { // Your validation logic
  3. $form->responseValidationMessages('title', 'Incorrect title format');
  4. // If there are multiple error messages, the second parameter can be passed as an array
  5. $form->responseValidationMessages('content', ['content format error', 'content cannot be empty']);
  6. }
  7. });

Front-end verification

The system inherits bootstrap-validator for front-end form validation and supports validation of H5 form types.

{tip} Browsers that don’t support H5 can also use front-end validation, and the system has been made compatible. Most forms support both front-end and back-end validation, both can work at the same time without conflict, a few forms front-end validation is invalid.

H5 verification

required

mandatory fields

  1. $form->text('title')->required();

number

Only numbers are allowed

  1. $form->text('age')->type('number');

limits

  1. // Only numbers in the range of 10-60 are allowed.
  2. $form->text('age')
  3. ->type('number')
  4. ->attribute('min', 10)
  5. ->attribute('max', 60);

email

email address

  1. $form->email('email');

url

links

  1. $form->text('website')->type('url');

Other

minLength

Limit minimum character length

  1. $form->text('title')->minLength(20);
  2. // Setting error messages
  3. $form->text('title')->minLength(20, 'Minimum of 20 characters');

maxLength

Limit the maximum length of characters

  1. $form->text('title')->maxLength(50);
  2. // Setting error messages
  3. $form->text('title')->maxLength(50, 'No more than 50 characters.');

same

Limits the value of the current field to be equal to the value of the given field.

  1. $form->password('password');
  2. $form->password('password_confirm')->same('password');
  3. // Setting error messages
  4. $form->password('password_confirm')->same('password', 'Two inconsistent password entries');

Custom

Developers can customize front-end validation rules in the following ways

Add the following code to app/Admin/bootstrap.php.

  1. use Dcat\Admin\Form\Field;
  2. Field\Text::macro('len', function (int $length, ?string $error = null) {
  3. // Extensions to the front-end verification logic
  4. Admin::script(
  5. <<<'JS'
  6. Dcat.validator.extend('len', function ($el) {
  7. return $el.val().length != $el.attr('data-len');
  8. });
  9. JS
  10. );
  11. // Also add back-end verification logic, depending on the need
  12. $this->rules('size:'.$length);
  13. return $this->attribute([
  14. 'data-len' => $length,
  15. 'data-len-error' => str_replace(
  16. [':attribute', ':len'],
  17. [$this->label, $length],
  18. $error ?: "Only :len characters can be entered."
  19. ),
  20. ]);
  21. });

use

  1. $form->text('name')->len(10);