Rules validation rule in Yii Cmodel

Source: Internet
Author: User
Tags dateformat

First, the case is as follows

Array( Array(' username ', ' required '),Array(' username ', ' length ', 'min' =>3, 'Max' =>12),Array(' Password ', ' compare ', ' compareattribute ' = ' password2′ ', ' on ' = ' register '),Array(' Password ', ' Authenticate ', ' on ' = ' login '),Array(' Price ', ' numerical ', ' integeronly ' =true),); Public functionrules () {return Array(      Array(' title, content, status ', ' required '),Array(' title ', ' Length ', 'Max' =>128),Array(' status ', ' in ', 'Range' =Array(+)),Array(' tags ', ' match ', ' pattern ' = '/^[\w\s,]+$/',' message '= = ' Tags can only contain word characters. '),Array(' tags ', ' normalizetags '),Array(' title, Status ', ' safe ', ' on ' = ' search '),  );}

II. pre-defined complete list

Boolean: Cbooleanvalidator alias, ensure that the value of the property is Cbooleanvalidator::truevalue or Cbooleanvalidator::falsevalue.Captcha: The Ccaptchavalidator alias ensures that the value of the attribute is equal to the verification code displayed by the CAPTCHA.Compare: The alias of Ccomparevalidator, which ensures that the value of the attribute is equal to another attribute or constant.Email: The alias of Cemailvalidator, which ensures that the value of the attribute is a valid e-mail address.default: An alias for the cdefaultvaluevalidator that assigns a default value to the attribute.exist: Cexistvalidator alias to ensure that the attribute value exists in the specified data table field.file: Cfilevalidator alias, ensuring that the feature contains the name of an uploaded file.Filter: Cfiltervalidator alias, using a Filter transform property.inch: The alias of Crangevalidator ensures that the feature appears in the list of values for a subscription.length: The alias of Cstringvalidator ensures that the length of the feature is within the specified range.Match: The alias of the Cregularexpressionvalidator ensures that the attribute matches a regular expression.Numerical: The alias of the Cnumbervalidator ensures that the feature is a valid number.Required: The alias of the Crequiredvalidator ensures that the attribute is not empty.type: The alias of Ctypevalidator, which ensures that the attribute is of the specified data type.Unique: The alias of Cuniquevalidator ensures that the attribute is unique in the Data table field.URL: The alias of the Curlvalidator ensures that the feature is a valid path

Iii. Methods of succession

Yii Validation Rulesit Classification: The rules validation of Yii Yii cvalidator main properties attributes, Builtinvalidators,enableclientvalidation,message,on,safe ,Skiponerror frequently used properties are attributes, Builtinvalidators,message,on these four below are the corresponding validation classes required:Crequiredvalidatorfilter:Cfiltervalidatormatch:Cregularexpressionvalidatoremail:Cemailvalidatorurl:Curlvalidatorunique:Cuniquevalidatorcompare:Ccomparevalidatorlength:Cstringvalidatorin:crangevalidatornumerical:Cnumbervalidatorcaptcha:Ccaptchavalidatortype:Ctypevalidatorfile:Cfilevalidatordefault:cdefaultvaluevalidatorexist:CexistvalidatorBoolean:CbooleanvalidatorDate:Cdatevalidatorsafe:Csafevalidatorunsafe: Cunsafevalidator
1, crequiredvalidator– must be value validation property Requiredvalue-Mixed-The desired value is strict-Boolean-whether to compare strict instances:Array(' username ', ' required '),cannot be emptyArray(' username ', ' required ', ' requiredvalue ' = ' lh ', ' message ' = ' usernmae must be LH '), this value must be LH,If you fill in another value, it will be verified, butArray(' username ', ' required ', ' requiredvalue ' = ' lh ', ' strict ' =true), strict validation can also be followed by a ' message ' = ' and ' on ' =these2, cfiltervalidator Filter Validation Property filter– the method name (invoking the user's own defined function) instance:Array(' username ', ' test ')functionTest () {$username=$this->username;if($username! = ' LH ') {$this->adderror (' username ',' username must be LH '); }} Use this method if you still write a message in the array= ",The information given is still in your test. Which is the error message in test.3, Cregularexpressionvalidator-the regular validation property allowempty– is empty (default true) not-whether to reverse the validation logic (default false) pattern– regular expression matches an instance://match A-Z array (' username ', ' match ', ' AllowEmpty ' =>true, ' pattern ' = '/[a-z]/i ', ' message ' = ' must be a letter '),// Match is not A-Z array (' username ', ' match ', ' AllowEmpty ' =>true, ' not ' =>true, ' pattern ' = '/[a-z]/i ', ' message ' = ') Must not be a letter '),4, cemailvalidator– Mailbox Validation properties: allowempty– Whether null allowname– allows the name of the e-mail address checkmx– whether to check MX records for e-mail addresses checkport– Whether to check the e-mail address of port 25 fullpattern– regular expression, which is used to validate the e-mail address with the name of a part of the pattern– regular expression that is used to validate the property value instance:Array(' username ', ' email ', ' message ' + ' must be e-mail ', ' pattern ' = '/[a-z]/i '), 5, Curlvalidator–url Validation property: allowempty– is empty defaultscheme– the default URI scheme pattern– regular expression validschemes– manifest should be considered a valid URI plan. Example:Array(' username ', ' url ', ' message ' = ' must URL '),Array(' username ', ' url ', ' defaultscheme ' = ' = ' http://www.baidu.com '),6, cuniquevalidator– Uniqueness Validation Property: allowempty– is empty attributename– property name casesensitive– case-sensitive classname– class name criteria– additional query conditions Example:Array(' username ', ' unique ', ' message ' = ' The record exists '),Array(' username ', ' unique ', ' casesensitive ' =false, ' message ' = ' The record exists '), 7, ccomparevalidator– Compare validation properties: allowempty– is an empty compareattribute– property to compare CompareValue-Compare the values of the operator– comparison operator strict– strictly validate (both values and types are equal) instances://compare with a value array (' username ', ' compare ', ' comparevalue ' = ' 10′ ', ' operator ' = ' > ', ' message ' = ' = ' must be greater than 10′ '),// Compare an array with a submitted attribute (' username ', ' compare ', ' Compareattribute ' =& gt; ') Password ', ' operator ' = ' > ', ' message ' = ' must be greater than password '),8, cstringvalidator– string validation property: allowempty– is an empty encoding– encoding is– the exact lengthMax– Maximum lengthmin– Minimum Length toolong– defines an error with a value that is too large tooshort– defines the minimum length of the error instance: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– The validation logic in a range of properties: Whether allowempty– is null not– is reversed. Range–array Range strict– strictly validate (the same type and value) instance: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 property: allowempty– is an empty integeronly– integer integerpattern– regular expression matches an integerMax– Maximum Valuemin– The minimum value numberpattern– matches the number toobig– error when the value is too large toosmall– the error message instance that the value is too small: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– authentication Code validation attribute: allowempty– is case-sensitive if it is empty casesensitive–12, ctypevalidator– type validation property: allowempty– is an empty dateformat– date should follow the format pattern (' MM/dd/yyyy ') datetimeformat– date time should follow the format pattern (' MM/DD/YYYY hh:mm ') timeformat– time should follow the format pattern (' HH:mm ') type– type 'string’, ‘integer’, ‘float’, ‘Array’, ‘Date’, ‘ Timeinstances of ' and ' datetime ':Array(' username ', ' type ', ' dateformat ' = ' mm/dd/yyyy ', ' type ' = ' = 'Date'), 13, cfilevalidator– File validation properties: allowempty– is empty maxfiles– maximum number of files maxsize– file maximum minsize– minimum toolarge– too large error message toomany– too much Error message when toosmall– error message types– allow error message when file extension wrongtype– extension is incorrect14, cdefaultvaluevalidator– Default Value property: setonempty– set to null value– default instance:Array(' username ', 'default', ' setonempty ' =true, ' value ' = ' lh '), 15, cexistvalidator– whether there is an attribute: AllowEmpty=is empty attributename– property name classname– class name criteria– standard16, cbooleanvalidator– Boolean type validation property: allowempty– is the value of the null falsevalue– Error state strict– strictly validates the value instance of the truevalue– true state:Array(' username ', 'Boolean', ' TrueValue ' =>1, ' falsevalue ' =>-1, ' message ' = ' The value must be 1 or-1′ ', 17, cdatevalidator– Date validation property: allowempty– is an empty format– date value should follow the format pattern timestampattribute– receive the parse result for the attribute name instance:Array(' username ', 'Date', ' format ' = ' mm-dd-yyyy ', ' message ' = ' must be mm-dd-yyyy '),

Rules validation rule in Yii Cmodel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.