Rules validation rule in Yii Cmodel

Source: Internet
Author: User
Tags dateformat

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 Function Rules ()
{
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 '),
);
} Predefined full 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.
  • in: 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

Yii Validation Rulesit Classification: The rules validation of Yii Yii cvalidator main properties attributes, Builtinvalidators,enableclientvalidation,message,on,safe , Skiponerror

The frequently used attributes are attributes,builtinvalidators,message,on these four

The following is the corresponding validation class

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:cdefaultvaluevalidator

Exist:cexistvalidator

Boolean:cbooleanvalidator

Date:cdatevalidator

Safe:csafevalidator

Unsafe:cunsafevalidator

1. crequiredvalidator– must be value validation property

requiredvalue-mixed-the desired value

Is the strict-boolean-more stringent?

Example: Array (' username ', ' required '), cannot be empty

Array (' username ', ' required ', ' requiredvalue ' = ' lh ', ' message ' = ' usernmae must be LH '), this value must be LH, if the other values are not verified but

Array (' username ', ' required ', ' requiredvalue ' = ' lh ', ' strict ' =>true), strictly verified can also be appended with ' message ' and ' = ' and ' on ' = these

2. Cfiltervalidator Filter Validation Properties

filter– Method Name (call user-defined function)

Instance:

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

Use this method if you still write message=> in the array, the message given is still in your test. Which is the error message in test.

3, cregularexpressionvalidator–

Regular verifies whether the 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:

Whether the allowempty– is empty

allowname– whether the name of the e-mail address is allowed

checkmx– check MX records for email addresses

checkport– If you want to check the e-mail address of Port 25

fullpattern– regular expression, used to verify the e-mail address and part of the name

pattern– Regular Expressions,

An instance of the attribute value used to verify: Array (' username ', ' email ', ' message ' = ' e-mail ', ' pattern ' = '/[a-z]/i '),

5. Curlvalidator–url Validation Properties:

Whether the allowempty– is empty

defaultscheme– the default URI scheme

pattern– Regular Expressions

The validschemes– list should be considered a valid URI plan.

Instance:

Array (' username ', ' url ', ' message ' = ' must URL '),

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

6. cuniquevalidator– Uniqueness Validation attribute:

Whether the allowempty– is empty

attributename– Property Name

casesensitive– Case Sensitive

classname– class Name

criteria– Additional query conditions

Instance:

Array (' username ', ' unique ', ' message ' = ' The record exists '),

Array (' username ', ' unique ', ' casesensitive ' =>false, ' message ' = ' The record exists '),

7. ccomparevalidator– Compare Validation Properties:

Whether the allowempty– is empty

compareattribute– properties that need to be compared

CompareValue-The value of the comparison

operator– comparison Operators

strict– rigorous validation (both values and types are equal)

Example://Compare with a value array (' username ', ' compare ', ' comparevalue ' = ' 10′ ', ' operator ' = ' > ', ' message ' = ' = ' must be greater than 10′ '),

Compare with a submitted attribute array (' username ', ' compare ', ' compareattribute ' = ' password ', ' operator ' = ' > ', ' message ' = ' must be greater than password '),

8. cstringvalidator– String Validation Properties:

Whether the allowempty– is empty

encoding– encoding

is– the exact length

max– Maximum Length

min– min. length

toolong– error defining a value that is too large

tooshort– defines the minimum length error

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

Whether the allowempty– is empty

not– whether the validation logic is reversed.

Range–array Range

strict– rigorous validation (same type and value)

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 ' in 1 2 3 4 5′),

10. cnumbervalidator– Digital Validation Properties:

Whether the allowempty– is empty

integeronly– integer

integerpattern– Regular Expressions Match integers

max– Maximum Value

min– Minimum value

numberpattern– Match number

Error message when the toobig– value is too large

toosmall– error message with too-small value

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 properties:

Whether the allowempty– is empty

casesensitive– Case Sensitive

12. ctypevalidator– Type Validation Properties:

Whether the allowempty– is empty

dateformat– date should follow the format pattern (' mm/dd/yyyy ')

datetimeformat– format pattern (' mm/dd/yyyy hh:mm ') to be followed by date time

timeformat– time should follow the format pattern (' hh:mm ')

type– type ' string ', ' integer ', ' float ', ' array ', ' Date ', ' Time ' and ' datetime '

Example: Array (' username ', ' type ', ' dateformat ' = ' mm/dd/yyyy ', ' type ' = ' date '),

13. cfilevalidator– File Validation Properties:

Whether the allowempty– is empty

maxfiles– Maximum number of files

Maximum value of the maxsize– file

minsize– Minimum value

Error message when the toolarge– is too large

toomany– Too much error message

toosmall– error message for too hours

types– allowed file name extensions

Error message given when wrongtype– extension error

14. cdefaultvaluevalidator– Default Value property:

setonempty– set to NULL

value– Default Value

Example: Array (' username ', ' default ', ' Setonempty ' =>true, ' value ' = ' lh '),

15. cexistvalidator– whether there are attributes:

AllowEmpty = is empty

attributename– Property Name

classname– class Name

criteria– Standard

16. cbooleanvalidator– Boolean Type validation property:

Whether the allowempty– is empty

falsevalue– the value of the error state

strict– rigorous validation

truevalue– Value of real state

Example: Array (' username ', ' boolean ', ' TrueValue ' =>1, ' falsevalue ' =>-1, ' message ' = ' = ' The value must be 1 or-1′),

17. cdatevalidator– Date Validation attribute:

Whether the allowempty– is empty

format– format pattern to which date values should be followed

timestampattribute– property names that receive parsing results

Example: 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.