This example describes the Yii2 validator (Validator) usage. Share to everyone for your reference, as follows:
Let's take a look at the validator's use.
Public Function Rules () { return [[ ' email ', ' password '], ' required '], [' Password ', ' string ', ' min ' =>6 ], ];}
As shown above, the validator is primarily used in the rules, validating the value of the property in the current model to check if a requirement is met.
The validator uses the format:
The written format is: [property to validate, validator name, validator parameter].
If you need to verify that the property is available for multiple arrays, the available strings for a property are also available as an array to represent.
Each property can have more than one validator, as the above password uses the required and string two validators.
Common validators:
Yii2 has built in a number of commonly used validators. All validators inherit the implementation from the base class Yii\validators\validator. I probably concluded that there are so many categories.
Number-Related:
integer--integer
Used to detect whether an attribute value is an integer, and the maximum, minimum, or value detection. (Yii\validators\numbervalidator)
double--floating Point
Used to detect whether the property value is a floating-point number, or decimal. (Yii\validators\numbervalidator)
number--Digital
This is exactly the same as the double above, with only 2 names. (Yii\validators\numbervalidator)
Format Related:
date--Date
Verifies that the property value is the correct date format. (Yii\validators\datevalidator)
email--Mail
Detects whether the property value is the correct e-mail format. (Yii\validators\emailvalidator)
Url--url
Used to determine whether the property value is the correct URL address. (Yii\validators\urlvalidator)
function Processing of values:
filter--Filter
This is the processing of attribute values. such as prefix attribute values, replace specific strings, and so on. (Yii\validators\filtervalidator)
trim--Cutting Edge
This is the processing of attribute values. Just remove the spaces on either side of the string, or the specified string. (Yii\validators\filtervalidator)
Upload file verification:
file--file
This is mainly to verify the uploaded files, such as format, size, and so on. (Yii\validators\filevalidator)
image--Pictures
This is similar to the file validator above, but is designed to verify the image. (Yii\validators\imagevalidator)
Judging comparison:
compare--comparison
Used to compare two attribute values, such as equality, greater than, less than comparison, and so on. (Yii\validators\comparevalidator)
in--inclusive (range)
Used to detect whether the property value is contained in the specified array. (Yii\validators\rangevalidator)
exist--exists
Used to detect whether this property value already exists in the data table. (Yii\validators\existvalidator)
unique--Uniqueness
This is similar to exist, which is used to detect whether a value is unique. (Yii\validators\uniquevalidator)
string--string
Determine the length of the attribute value, such as the maximum length, the shortest length, and so on. (Yii\validators\stringvalidator)
boolean--Boolean type
Used to check whether the value of the property is a Boolean value. (Yii\validators\booleanvalidator)
default--Default Value
This is used to set the default value for the property. If the property value is null, set it to the default value of NULL. (Yii\validators\defaultvaluevalidator)
required--must be filled
This is used to check if the property value is empty. (Yii\validators\requiredvalidator)
captcha--Verification Code
This is mainly to verify the verification code when the interface uses the verification code. (Yii\captcha\captchavalidator)
match--Regular Expressions
This is more powerful to detect if the attribute value matches the given regular. The basics listed above can be implemented with this. (Yii\validators\regularexpressionvalidator)
Other:
safe--Safety
This is not verified and is only used to specify that the property value is safe. (Yii\validators\safevalidator)
For more information on YII related content readers can view this site topic: "YII framework Introduction and common skills Summary", "PHP Excellent Development Framework Summary", "Smarty Template Primer Basic Tutorial", "PHP Object-oriented Programming tutorial", "PHP string (String) Usage Summary "," Introduction to Php+mysql Database Operation "and" PHP common database Operation Skills Summary "
It is hoped that this article is helpful to the PHP program design based on YII framework.