The example in this article describes the Yii2 Validator (Validator) usage. Share to everyone for your reference, specific as follows:
First look at the use of the validator.
Public Function rules () {return [[
' email ', ' password '], ' required '],
[' Password ', ' string ', ' min ' = >6],
];
}
As shown above, the validator is primarily used in the rules to validate the value of the property in the current model to check whether a requirement is met.
Validator Use Format:
The writing format is: [Properties to be validated, validator names, validator parameters].
If you need to validate a property that is a number of available arrays, you can also use an array if you want to use a string for a property.
Each property can have multiple validators, such as the above password using required and string two validators.
Common validators:
Yii2 already has some commonly used validators built in. All validators inherit the implementation from the base class Yii\validators\validator. I've probably summed up a few of these categories.
Number Related:
integer--integer
used to detect whether a property value is an integer, as well as maximum, minimum detection, and so on. (Yii\validators\numbervalidator)
double--floating-point
used to detect whether a property value is a floating-point number, or a decimal number. (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
Verify that the property value is in 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)
To function on a value:
filter--Filter
This is the processing of the attribute value. such as prefixing attribute values, replacing specific strings, and so on. (Yii\validators\filtervalidator)
trim--Cutting Edge
This is the processing of the attribute value. Just remove the space on either side of the string, or the specified string. (Yii\validators\filtervalidator)
Upload file verification:
file--file
this is mainly to upload the file for verification, such as format, size and so on. (Yii\validators\filevalidator)
image--Pictures
This is similar to the file validator above, but is specifically used to validate pictures. (Yii\validators\imagevalidator)
Judgment comparison:
compare--comparison
used to compare two property values, such as equality, greater than, less than, and so on. (Yii\validators\comparevalidator)
in--contains (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 datasheet. (Yii\validators\existvalidator)
unique--Uniqueness
This is similar to exist and is used to detect whether a value is unique. (Yii\validators\uniquevalidator)
string--string
to determine the length of the attribute value, such as the maximum length, the shortest length. (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, it is set to a null default value. (Yii\validators\defaultvaluevalidator)
required--must fill
This is used to check whether the property value is empty. (Yii\validators\requiredvalidator)
captcha--Verification Code
this is mainly in the interface with the verification code when the verification code to verify. (Yii\captcha\captchavalidator)
match--Regular Expression
This is more powerful, and is used to detect whether the attribute value matches the given regular. The basics listed above can be implemented with this. (Yii\validators\regularexpressionvalidator)
Other:
safe--Security
this is not validated and is used only to specify that the property value is safe. (Yii\validators\safevalidator)
For more information on YII-related content, readers who are interested in this site can view the topics: Introduction to YII Framework and summary of common skills, "Summary of PHP Excellent development framework", "Smarty Template Introductory Course", "Introduction to PHP object-oriented programming", "PHP string" Summary of Usage , "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"
I hope this article will help you with the PHP program design based on the YII framework.