Rules verification rules in yii cmodel

Source: Internet
Author: User
Tags dateformat valid email address mx record
array(

Array ('username', 'required '),
Array ('username', 'length', 'Min' => 3, 'max '=> 12 ),
Array ('Password', 'company', 'companyattribute '=> 'password2', 'on' => 'register '),
Array ('Password', 'authenticate', 'on' => 'login '),

Array ('price', 'Numerical ', 'integeronly' => true ),
);
Public Function rules ()
{
Return array (
Array ('title, content, status', 'required '),
Array ('title', 'length', 'Max' => 128 ),
Array ('status', 'in', 'range' => array (1, 2, 3 )),
Array ('tags', 'match', 'pattern' => '/^ [\ W \ s,] + $ /',
'Message' => 'tags can only contain word characters .'),
Array ('tags', 'normalizetags '),
Array ('title, status', 'safe ', 'on' => 'search '),
);
} Complete predefined list:
  • boolean: Cbooleanvalidator alias. Make sure the attribute value is cbooleanvalidator: truevalue or cbooleanvalidator: falsevalue.
  • captcha: The alias of ccaptchavalidator to ensure that the feature value is equal to the Verification code displayed by CAPTCHA.
  • compare: The alias of ccomparevalidator to ensure that the feature value is equal to another feature or constant.
  • email: The alias of cemailvalidator, which ensures that the feature value is a valid email address.
  • default: Cdefavaluvaluevalidator alias, which assigns a default value for the feature.
  • exist: The alias of cexistvalidator to ensure that the attribute value exists in the specified data table field.
  • file: The alias of cfilevalidator to ensure that the feature contains the name of an uploaded file.
  • filter: Cfiltervalidator alias, which uses a filter conversion attribute.
  • in: The alias of crangevalidator to ensure that the feature appears in a reserved value list.
  • length: The alias of cstringvalidator to ensure that the feature length is within the specified range.
  • match: The alias of cregularexpressionvalidator to ensure that the feature matches a regular expression.
  • numerical: The alias of cnumbervalidator, which ensures that the feature is a valid number.
  • required: The alias of crequiredvalidator to ensure that the feature is not empty.
  • type: The alias of ctypevalidator, which ensures that the feature is of the specified data type.
  • unique: The alias of cuniquevalidator, which ensures that the feature is unique in the data table fields.
  • url: The alias of curlvalidator to ensure that the feature is a valid path.

Yii verification rulesIt classification: yii rules verification cvalidator main attributes, builtinvalidators, enableclientvalidation, message, on, safe, skiponerror

Commonly used attributes include attributes, builtinvalidators, message, and on.

Below are the corresponding verification classes

Required: crequiredvalidator

Filter: cfiltervalidator

Match: cregularexpressionvalidator

Email: cemailvalidator

URL: curlvalidator

Unique: cuniquevalidator

Compare: ccomparevalidator

Length: cstringvalidator

In: crangevalidator

Numerical: cnumbervalidator

CAPTCHA: ccaptchavalidator

Type: ctypevalidator

File: cfilevalidator

Default: cdefavaluvaluevalidator

Exist: cexistvalidator

Boolean: cbooleanvalidator

Date: cdatevalidator

Safe: csafevalidator

Unsafe: cunsafevalidator

1. crequiredvalidator-required value verification attribute

Requiredvalue-mixed-required value

Strict-Boolean-strict or not

Instance: array ('username', 'required'), cannot be empty

Array ('username', 'required', 'requiredvalue '=> 'lh', 'message' => 'usernmae must be LH '), the value must be LH, if you enter another value, the verification will fail.

Array ('username', 'required', 'requiredvalue '=> 'lh', 'strict '=> true ), strict verification can also be followed by 'message' => ", 'on' =>

2. cfiltervalidator filters verification attributes

Filter-method name (call a user-defined function)

Instance:

Array ('username', 'test') function test () {$ username = $ this-> username; if ($ username! = 'Lh ') {$ this-> adderror ('username', 'username must be LH ');}}

If you still write message => in array, the prompt is still in your test. This is based on the error information in test.

3. cregularexpressionvalidator-

Regular Expression verification attribute allowempty-whether it is null (true by default)

Not-whether to reverse the verification logic (default false) pattern-Regular Expression matching 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-Email verification attributes:

Allowempty-whether it is null

Allowname-whether the email address name is allowed

Checkmx-whether to check the MX record of the email address

Checkport-whether to check the email address on port 25

Fullpattern-Regular Expression used to verify part of the email address and name

Pattern-Regular Expression,

Example of the property value used for verification: array ('username', 'email ', 'message' =>' must be an email address ', 'pattern' => '/[A-Z]/I '),

5. curlvalidator-URL verification attributes:

Allowempty-whether it is null

Defaultscheme-default URI Scheme

Pattern-Regular Expression

Validschemes-the list should be considered as a valid URI plan.

Instance:

Array ('username', 'url', 'message' => 'ust url '),

Array ('username', 'url', 'defaultscheme '=> 'HTTP: // www.baidu.com '),

6. cuniquevalidator-unique verification attributes:

Allowempty-whether it is null

Attributename-attribute name

Casesensitive-case sensitive

Classname-class name

Criteria-additional query Conditions

Instance:

Array ('username', 'unique', 'message' => 'this record exists '),

Array ('username', 'unique', 'casesensitive '=> false, 'message' => 'this record exist '),

7. ccomparevalidator-comparison verification attributes:

Allowempty-whether it is null

Compareattribute-attributes to be compared

Comparevalue-value of comparison

Operator-comparison operator

Strict-strict verification (values and types must be equal)

Instance: // compare with a value. Array ('username', 'compare', 'comparevalue '=> '10', 'operator' => ', 'message' => 'must be greater than 10 ′),

// Compare it with a submitted attribute array ('username', 'compare', 'companyattribute '= & gt; 'Password', 'operator' => ', 'message' => 'must be greater than password '),

8. cstringvalidator-string verification attributes:

Allowempty-whether it is null

Encoding-Encoding

Is-exact length

Max-maximum length

Min-minimum length

Toolong-an error occurs when the defined value is too large.

Tooshort-An error occurred while defining the minimum length.

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-attributes within a certain range:

Allowempty-whether it is null

Not-whether to reverse the verification logic.

Range-array range

Strict-strict verification (the type and value must be the same)

Example: array ('username', 'in', 'range' => array (1, 2, 3, 4, 5 ), 'message' => 'ust 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-digit verification attributes:

Allowempty-whether it is null

Integeronly-integer

Integerpattern-Regular Expression matches integer

Max-Maximum

Min-Min

Numberpattern-matching number

Error message when toobig-value is too large

Error message when toosmall-value is too small

Instance: array ('username', 'Numerical ', 'integeronly' => true, 'message' => 'ust be int '),

Array ('username', 'Numerical ', 'integeronly' => true, 'message' => 'ust be int', 'max' => 100, 'Min' => 10, 'toobig '=>' is too big ', 'toosmall' =>' is too small '),

11. ccaptchavalidator-verification code verification attributes:

Allowempty-whether it is null

Casesensitive-case sensitive

12. ctypevalidator-type verification attributes:

Allowempty-whether it is null

Dateformat-the format pattern to be followed by the date ('Mm/DD/yyyy ')

Datetimeformat-the format pattern to be followed by the date and time ('Mm/DD/YYYY hh: mm ')

Timeformat-the format that the time should follow ('hh: mm ')

Type-type 'string', 'integer', 'float', 'array', 'date', 'time', and 'datetime'

Instance: array ('username', 'type', 'dateformat' => 'Mm/DD/yyyy', 'type' => 'date '),

13. cfilevalidator-file verification attributes:

Allowempty-whether it is null

Maxfiles-Maximum number of files

Maxsize-maximum value of a file

Minsize-Minimum value

Toolarge-error message when toolarge is too large

Toomany-too many error messages

Toosmall-Too hour error message

Types-allowed file extensions

Wrongtype-error message given when the extension is incorrect

14. cdefavaluvaluevalidator-default property:

Setonempty-set to null

Value-default value

Instance: array ('username', 'default', 'setonempty' => true, 'value' => 'lh '),

15. Whether the cexistvalidator attribute exists:

Allowempty = NULL

Attributename-attribute name

Classname-class name

Criteria-Standard

16. cbooleanvalidator-boolean type verification attributes:

Allowempty-whether it is null

Falsevalue-value of the error status

Strict-strict verification

Truevalue-value of the true state

Instance: array ('username', 'boolean', 'truevalue '=> 1, 'falsevalue' =>-1, 'message' => 'the value must be 1 or-1 ′),

17. cdatevalidator-date verification attributes:

Allowempty-whether it is null

Format-the Format mode to be followed by the date value

Timestampattribute-name of the property that receives the resolution result

Instance: array ('username', 'date', 'format' => 'Mm-dd-yyyy ', 'message' => 'ust be mm-dd-yyyy '),

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.