This article mainly introduces the usage of the Yii2 validators (Validator), and analyzes the functions, formats, usage methods and precautions of the Yii2 validators (Validator) in combination with examples, for more information about the Yii2 Validator usage, see the following example. We will share this with you for your reference. The details are as follows:
Let's take a look at the use of the validators.
public function rules(){ return [ [['email', 'password'], 'required'], ['password', 'string', 'min'=>6], ];}
As shown above, the validators are mainly used in rules to verify the attribute values in the current model to check whether certain requirements are met.
Format used by the validators:
The format is as follows: [attributes to be verified, validators name, validators parameters].
If the attribute to be verified has multiple available arrays, an array can be used to represent the attribute as a string.
Each attribute can have multiple validators. the above password uses two validators: required and string.
Common validators:
Yii2 has built-in some common validators. All validators are inherited from the base class yii \ validators \ Validator. I will summarize the following categories.
Digit related:
Integer -- integer
It is used to detect whether the attribute value is an integer, as well as the maximum and minimum values. (Yii \ validators \ NumberValidator)
Double -- floating point
It is used to check whether the attribute value is a floating point number, that is, a decimal number. (Yii \ validators \ NumberValidator)
Number -- number
This is exactly the same as the double above, but only two names are used. (Yii \ validators \ NumberValidator)
Format:
Date -- date
Check whether the property value is in the correct date format. (Yii \ validators \ DateValidator)
Email-email
Check whether the attribute value is in the correct email format. (Yii \ validators \ EmailValidator)
Url -- URL
Used to determine whether the attribute value is a correct url address. (Yii \ validators \ UrlValidator)
Perform function processing on values:
Filter -- filter
This process processes the attribute values. For example, adding a prefix to a property value and replacing a specific string. (Yii \ validators \ FilterValidator)
Trim -- edge cutting
This process processes the attribute values. Only remove spaces on both sides of the string or the specified string. (Yii \ validators \ FilterValidator)
Upload file verification:
File -- file
This mainly verifies the uploaded file, such as the format and size. (Yii \ validators \ FileValidator)
Image -- image
This is similar to the file validator above, but it is used to verify the image. (Yii \ validators \ ImageValidator)
Comparison:
Compare -- Comparison
Compares two attribute values, such as equal, greater than, or less. (Yii \ validators \ CompareValidator)
In -- include (range)
Used to check whether attribute values are included in the specified array. (Yii \ validators \ RangeValidator)
Exist -- exist
Used to check whether the attribute value exists in the data table. (Yii \ validators \ ExistValidator)
Unique -- Uniqueness
This is similar to exist and is used to check whether the value is unique. (Yii \ validators \ UniqueValidator)
String -- string
Determine the length of the attribute value, such as the maximum length and the minimum length. (Yii \ validators \ StringValidator)
Boolean -- boolean
Used to check whether the attribute value is a Boolean value. (Yii \ validators \ BooleanValidator)
Default -- default value
This is used to set the default value for the property. For example, if the attribute value is null, set it to the default value of null. (Yii \ validators \ DefaultValueValidator)
Required -- required
This is used to check whether the property value is null. (Yii \ validators \ RequiredValidator)
Captcha-verification code
The verification code is verified when the verification code is used on the interface. (Yii \ captcha \ CaptchaValidator)
Match -- Regular expression
This is more powerful, used to detect whether the attribute value matches the regular expression given. This implementation can be basically used for all the items listed above. (Yii \ validators \ RegularExpressionValidator)
Others:
Safe -- security
This is not verified. it is only used to specify the attribute value to be safe. (Yii \ validators \ SafeValidator)