Home >Backend Development >PHP Tutorial >Detailed explanation of Laravel framework form validation, laravel framework form_PHP tutorial
Basic verification example
The first parameter passed to the make function is the data to be verified, and the second parameter is the verification rule that needs to be applied to the data.
Multiple validation rules can be separated by the "|" character, or as a single element of an array.
Specify validation rules via array
Once a Validator instance is created, this validation can be performed using the fails (or passes) function.
Document Verification
The Validator class provides some validation rules for validating files, such as size, mimes, etc. When validating a file, you can pass it to the validator just like any other validation.
With error message
After calling the messages function on a Validator instance, you will get a MessageBag instance, which has many convenient functions for handling error messages.
Get the first error message for a domain
Get all error messages for a domain
Get all error messages for all domains
Check if messages exist for a domain
:message
'); Get all error messages in a certain format
Error Messages & Views
Once you've performed validation, you need an easy way to feed error messages back to the view. This can be easily handled in Lavavel. Take the following route as an example:
However, note that we do not have to explicitly bind the error message to the route in the GET route. This is because Laravel always checks the Session for errors and automatically binds them to the view if they are available. So, for every request, a $errors variable is always available in all views, allowing you to conveniently assume that $errors is always defined and safe to use. The $errors variable will be an instance of the MessageBag class.
So, after the jump, you can use the auto-bound $errors variable in the view:
Here is a list of all available validation rules and their functions:
accepted
Validate that the value of this rule must be yes, on, or 1. This is useful when verifying agreement with the Terms of Service.
active_url
Verifies that the value of this rule must be a valid URL, according to the PHP function checkdnsrr.
after:date
Validates that the value of this rule must be after the given date, which will be passed via the PHP function strtotime.
alpha
The value validating this rule must consist entirely of alphabetic characters.
alpha_dash
Validate that the value of this rule must consist entirely of letters, numbers, dashes, or underline characters.
alpha_num
The value validating this rule must consist entirely of letters and numbers.
before:date
Validates that the value of this rule must be before the given date, which will be passed through the PHP function strtotime.
between:min,max
Validates that the value of this rule must be between the given min and max. Strings, numbers, and files are compared using size rules.
confirmed
The value of this validation rule must be the same as the value of foo_confirmation. For example, if the field that needs to be validated for this rule is password, there must be an identical password_confirmation field in the input.
date
Validates that the value of this rule must be a valid date, according to the PHP function strtotime.
date_format:format
Validates that the value of this rule must conform to the given format, according to the PHP function date_parse_from_format.
different:field
Validates that the value of this rule must be different from the value of the specified field.
email
Validate that the value of this rule must be a valid email address.
exists:table,column
Validates that the value of this rule must exist in the specified database table.
Basic usage of Exists rules
image
The value validating this rule must be an image (jpeg, png, bmp or gif).
in:foo,bar,...
Validates that the value of this rule must exist in the given list.
integer
Validate that the value of this rule must be an integer.
Verify that the value of this rule must be a valid IP address.
max:value
Validate that the value of this rule must be less than the maximum value. Strings, numbers, and files are compared using size rules.
mimes:foo,bar,...
The MIME type of the file validating this rule must be in the given list.
Basic use of MIME rules
min:value
The value that validates this rule must be greater than the minimum value. Strings, numbers, and files are compared using size rules.
not_in:foo,bar,...
Validates that the value of this rule must not exist in the given list.
numeric
Validate that the value of this rule must be a number.
regex:pattern
Validates that the value of this rule must match the given regular expression.
Note: When using regex mode, it is necessary to use an array to specify the rules instead of a pipe delimiter, especially when the regular expression contains a pipe character.
required
Validate that the value of this rule must exist in the input data.
required_if:field,value
When the specified field is a certain value, the value of this rule must exist.
required_with:foo,bar,...
Validate that the value of this rule must exist only if the specified domain exists.
required_without:foo,bar,...
Validate that the value of this rule must exist only if the specified domain does not exist.
same:field
Validate that the value of this rule must be the same as the value of the given domain.
size:value
Validates that the value of this rule must be the same size as the given value. For strings, value represents the number of characters; for numbers, value represents its integer value; for files, value represents the size of the file in KB.
unique:table,column,except,idColumn
Validates that the value of this rule must be unique within the given database table. If column is not specified, the name of the field will be used.
Basic use of Unique rules
url
Validate that the value of this rule must be a valid URL.
Customized error message
If necessary, you can use a custom error message instead of the default one. There are several ways to customize error messages.
Pass custom message to validator
Note: The :attribute placeholder will be replaced by the name of the actual domain being validated. You can also use other placeholders in the error message.
Other validation placeholders
Specify customized error messages for a specific domain
Specify error message in language file
Customized verification rules
Laravel provides a range of useful validation rules; however, you may wish to add your own. One way is to register custom validation rules using the Validator::extend function:
Register a custom validation rule
Note: The name of the rule passed to the extend function must comply with the "snake cased" naming rule.
The custom validator accepts three parameters: the name of the attribute to be verified, the value of the attribute to be verified, and the parameters passed to this rule.
You can also pass a class function to the extend function instead of using a closure:
You can also extend the Validator class itself instead of extending the validator with a closure callback. To achieve this, add a validator class that inherits from IlluminateValidationValidator. You can add a validation function starting with validate in the class:
Extended validator class
Next, you need to register your custom validator extension:
You need to register a custom validator extension
When creating a custom validation rule, you sometimes need to define a custom placeholder for the error message. To achieve this, you can create a custom validator like above and add a replaceXXX function in the validator:
You can write a special summary of methods.
For example: