<span>array</span><span>( </span><span>array</span>(‘username’, ‘required’), <span>array</span>(‘username’, ‘length’, ‘<span>min</span>’=>3, ‘<span>max</span>’=>12), <span>array</span>(‘password’, ‘compare’, ‘compareAttribute’=>’password2′, ‘on’=>’register’), <span>array</span>(‘password’, ‘authenticate’, ‘on’=>’login’), <span>array</span>(‘Price’,’numerical’, ‘integerOnly’=><span>true</span>),<span> ); </span><span>public</span> <span>function</span><span> rules() { </span><span>return</span> <span>array</span><span>( </span><span>array</span>(‘title, content, status’, ‘required’), <span>array</span>(‘title’, ‘length’, ‘<span>max</span>’=>128), <span>array</span>(‘status’, ‘in’, ‘<span>range</span>’=><span>array</span>(1,2,3)), <span>array</span>(‘tags’, ‘match’, ‘pattern’=>’/^[\w\s,]+$/’,<span> ‘message’</span>=>’Tags can only contain word characters.’), <span>array</span>(‘tags’, ‘normalizeTags’), <span>array</span>(‘title, status’, ‘safe’, ‘on’=>’search’),<span> ); }</span>
Full list of predefined:
boolean
: Alias of CBooleanValidator, make sure the value of the property is CBooleanValidator::trueValue or CBooleanValidator::falseValue.
captcha
: Alias of CCaptchaValidator, ensuring that the value of the attribute is equal to the verification code displayed by CAPTCHA.
compare
: Alias of CCompareValidator, ensuring that the value of a property is equal to another property or constant.
email
: Alias for CEmailValidator, ensuring that the value of the attribute is a valid email address.
default
: Alias of CDefaultValueValidator, assigns a default value to the attribute.
exist
: Alias of CExistValidator, ensures that the attribute value exists in the specified data table field.
file
: Alias for CFileValidator, ensuring that the attribute contains the name of an uploaded file.
filter
: Alias of CFilterValidator, using a filter to convert attributes.
in
: Alias for CRangeValidator, ensuring that the attribute appears in a predetermined list of values.
length
: Alias of CStringValidator, ensuring that the length of the attribute is within the specified range.
match
: Alias of CRegularExpressionValidator, ensuring that the feature matches a regular expression.
numerical
: Alias of CNumberValidator, ensuring that the attribute is a valid number.
required
: Alias of CRequiredValidator, ensuring that the attribute is not empty.
type
: Alias of CTypeValidator, ensuring that the attribute is the specified data type.
unique
: Alias of CUniqueValidator, ensuring that the characteristics are unique in the data table field.
url
: Alias of CUrlValidator, ensuring that the feature is a valid path
yii verification rulesit Category: Yii yii rules verification cValidator main attributes attributes, builtInValidators, enableClientValidation, message, on, safe, skipOnError
The commonly used attributes are attributes, builtInvalidators, message, on.
The following is the corresponding verification class
required: CRequiredValidator
filter: CFilterValidator
match: CRegularExpressionValidator
email: CEmailValidator
url: CUrlValidator
unique: CUniqueValidator
compare: CCompareValidator
length: CStringValidator
in: CRangeValidator
numeric: CNumberValidator
captcha: CCaptchaValidator
type: CTypeValidator
file: CFileValidator
default: CDefaultValueValidator
exist: CExistValidator
boolean: CBooleanValidator
date: CDateValidator
safe: CSafeValidator
unsafe: CUnsafeValidator
1. CRequiredValidator – required value validation attribute
requiredValue-mixed - the required value
strict-boolean-whether it is strict
Example: array(‘username’, ‘required’), cannot be empty
array('username', 'required', 'requiredValue'=>'lh','message'=> 'usernmae must be lh'), this value must be lh, if you fill in other values It will still be verified
array('username', 'required', 'requiredValue'=>'lh', 'strict'=>true), for strict verification, you can also add 'message'=>" at the end, 'on'=>These
2. CFilterValidator filter verification attribute
filter – method name (call user-defined function)
Example:
array('username', 'test') function test() { $username = $this->username; if($username != 'lh'){ $this->addError(' username', 'username must be lh'); } }
Using this method, if you still write message=>" in the array, the prompt information given is still in your test. That is, the error message in the test shall prevail
3. CRegularExpressionValidator -
Regular verification attribute allowEmpty – whether it is empty (default true)
not-whether to reverse the verification logic (default false) pattern – regular expression matching example:
// Match a-z array('username', 'match', 'allowEmpty'=>true, 'pattern'=>'/[a-z]/i','message'=>' Must be letters'),
//The match is not a-z array('username', 'match', 'allowEmpty'=>true, 'not'=>true, 'pattern'=>'/[a-z]/i ','message'=>'Must not be a letter'),
4. CEmailValidator – Email verification attributes:
allowEmpty – whether it is empty
allowName – whether to allow the name in the email address
checkMx – Whether to check the MX record of the email address
checkPort – Whether to check port 25 email address
fullPattern – Regular expression used to validate part of email addresses and names
pattern – regular expression,
Examples of attribute values used for verification: array('username', 'email', 'message'=>'Must be email', 'pattern'=>'/[a-z]/i '),
5. CUrlValidator – url verification attribute:
allowEmpty – whether it is empty
defaultScheme – The default URI scheme
pattern – regular expression
validSchemes – Manifests should be considered valid URI schemes.
Example:
array(‘username’, ‘url’, ‘message’=>’must url’),
array(‘username’, ‘url’, ‘defaultScheme’=>’http://www.baidu.com’),
6. CUniqueValidator – uniqueness verification attribute:
allowEmpty – whether it is empty
attributeName – attribute name
caseSensitive – case sensitive
className – class name
criteria – additional query criteria
Example:
array(‘username’, ‘unique’, ‘message’=>’This record exists’),
array(‘username’, ‘unique’, ‘caseSensitive’=>false, ‘message’=>’The record exists’),
7. CCompareValidator – Compare validation attributes:
allowEmpty – whether it is empty
compareAttribute – the attribute that needs to be compared
compareValue - the value to compare
operator – comparison operator
strict – Strict verification (value and type must be equal)
Example: // Compare with a certain value array('username', 'compare', 'compareValue'=>'10′, 'operator'=>'>', 'message'= >'must be greater than 10′),
// Compare with a submitted attribute array('username', 'compare', 'compareAttribute'=& gt;'password', 'operator'=>'>', 'message' =>'Must be greater than password'),
8. CStringValidator – String validation attribute:
allowEmpty – whether it is empty
encoding – encoding
is – the exact length
max – Maximum length
min – Minimum length
tooLong – error in defining a value that is too large
tooShort – Error in defining minimum length
Example: array('username', 'length', 'max'=>10, 'min'=>5, 'tooLong'=>'too long', 'tooShort'= >'Too short'),
array(‘username’, ‘length’, ‘is’=>5, ‘message’=>’ length must be 5′),
9. CRangeValidator – Attributes within a certain range:
allowEmpty – whether it is empty
not – Verification logic of whether to reverse or not.
range – array range
strict – Strict verification (type and value must be the same)
Example: array('username', 'in', 'range'=>array(1,2,3,4,5), 'message'=>'must in 1 2 3 4 5′),
array('username', 'in', 'not'=>true, 'range'=>array(1,2,3,4,5), 'message'=>' must not in 1 2 3 4 5′),
10. CNumberValidator – Numeric validation attribute:
allowEmpty – whether it is empty
integerOnly – Integer
integerPattern – Regular expression matching integers
max – maximum value
min – minimum value
numberPattern – matching number
tooBig – error message when the value is too big
tooSmall – error message if the value is too small
Example: array(‘username’, ‘numerical’, ‘integerOnly’=>true, ‘message’=>’must be int’),
array('username', 'numerical', 'integerOnly'=>true, 'message'=>'must be int', 'max'=>100, 'min'=> 10, 'tooBig'=>'is too big', 'tooSmall'=>'is too small'),
11. CCaptchaValidator – Verification code verification attribute:
allowEmpty – whether it is empty
caseSensitive – case sensitive
12. CTypeValidator – Type verification attribute:
allowEmpty – whether it is empty
dateFormat – the format pattern that the date should follow (‘MM/dd/yyyy’)
datetimeFormat – the format pattern that date and time should follow (‘MM/dd/yyyy hh:mm’)
timeFormat – the format pattern that time should follow (‘hh:mm’)
type – types ‘string’, ‘integer’, ‘float’, ‘array’, ‘date’, ‘time’ and ‘datetime’
Example: array(‘username’, ‘type’, ‘dateFormat’=>’MM/dd/yyyy’, ‘type’=>’date’),
13. CFileValidator – File verification attributes:
allowEmpty – whether it is empty
maxFiles – Maximum number of files
maxSize – the maximum size of the file
minSize – minimum value
tooLarge – error message when too large
tooMany – Error message when there are too many
tooSmall – too small error message
types – allowed file extensions
wrongType – the error message given when the extension is wrong
14. CDefaultValueValidator – Default value attribute:
setOnEmpty – set to empty
value – Default value
Example: array(‘username’, ‘default’, ‘setOnEmpty’=>true, ‘value’=>’lh’),
15. CExistValidator – Whether the attribute exists:
allowEmpty = Whether it is empty
attributeName – attribute name
className – class name
criteria – Standard
16. CBooleanValidator – Boolean type verification attribute:
allowEmpty – whether it is empty
falseValue – the value of the error status
strict – strict verification
trueValue – the value of the true state
Example: array('username', 'boolean', 'trueValue'=>1, 'falseValue'=>-1, 'message'=>'the value must be 1 or -1 ′),
17. CDateValidator – Date validation attribute:
allowEmpty – whether it is empty
format – the format pattern that the date value should follow
timestampAttribute – the attribute name that receives the parsing result
Example: array('username', 'date', 'format'=>'MM-dd-yyyy','message'=>'must be MM-dd-yyyy'),
yxmhero1989.blog.163.com/...61864/
This article describes the implementation of Yii verification code. It is just a small example of the author's application. It is also available on the Internet. To summarize, I hope to help Yii enthusiasts in need. 1. The author uses user login, so I add the following code to the sitecontroller: public function actions()
{
return array(
// captcha action renders the CAPTCHA image displayed on the contact page
'captcha'=>array(
'class'=>'CCaptchaAction',
'backColor'=>0xFFFFFF, //Background color
'minLength'=>4, // The shortest is 4 digits
'maxLength'=>4, //The longest is 4 digits
'transparent'=>true, //The display is transparent. When this option is turned off, the background color is displayed
),
);
}
2. Add the following code to the form file (view file such as login.php):
< div>
< ?php echo $form->labelEx($model,'verifyCode'); ?>
< div>
< ; ?php $this->widget('CCaptcha'); ?>
< ?php echo $form->textField($model,'verifyCode'); ?>
< / div>
< div>Enter verification code