YII2 Rules Validation Rule

Source: Internet
Author: User

Rules Validation rule:

Required : Mandatory value validation Attribute | | The alias of the Crequiredvalidator ensures that the attribute is not empty.

[[' Field name 1 ', ' Field Name 2 '],required]//Field 1 2 Required [[' Field name '],required, ' requiredvalue ' = ' required value ', ' message ' = ' message '];

Email : Email Verification | | The alias of Cemailvalidator ensures that the value of the attribute is a valid e-mail address.

[' email ', ' email '];

match : Regular validation | | The alias of the Cregularexpressionvalidator ensures that the attribute matches a regular expression.

[[' Field name '], ' match ', ' pattern ' = ' regular expression ', ' message ' = ' hint message '];  [[' Field name '], ' match ', ' not ' =>ture, ' pattern ' = ' regular expression ', ' message ' = ' hint message ']; /* Regular Take counter */

URL : URL | | The alias of the Curlvalidator ensures that the feature is a valid path.

[' website ', ' url ', ' defaultscheme ' = ' http '];

Captcha(verification code) | | The Ccaptchavalidator alias ensures that the value of the attribute is equal to the verification code displayed by the CAPTCHA.

[' Verificationcode ', ' captcha '];

Safe: Secure

[' Description ', ' safe '];

Compare :(Compare) The alias of the Ccomparevalidator, ensuring that the value of the attribute equals another attribute or constant.

[' Repassword ', ' compare ', ' compareattribute ' = ' password ', ' message ' = ' two ' password inconsistent! '],//comparevalue: Compare constant value operator: comparison operator [' age ', ' compare ', ' comparevalue ' = ' = ' operator '];

default: Defaults | | An alias for the cdefaultvaluevalidator that assigns a default value to the attribute.

[' Age ', ' Default ', ' value ' = null];

exist : Presence | | A cexistvalidator alias that ensures that the attribute value exists in the specified data table field.

[' Field name ', ' exist '];

file: Files | | The alias of the Cfilevalidator ensures that the feature contains the name of an uploaded file.

[' Primaryimage ', ' file ', ' extensions ' = [' png ', ' jpg ', ' gif '], ' maxSize ' and 1024*1024*1024]

Filter : Filter | | Cfiltervalidator alias, use a Filter transform property.

' Skiponarray ' = true is not required [[' username ', ' email '], ' filter ', ' filter ' = ' trim ', ' skiponarray ' and ' = true];

in: Range | | The alias of the Crangevalidator ensures that the feature appears in the list of values for a subscription.

[' Level ', ' in ', ' range ' = [1, 2, 3]];

unique: Uniqueness | | The alias of the Cuniquevalidator ensures that the attribute is unique within the data table field.

[' Field name ', ' unique ']

Supplemental : Federated unique index rule rule

[' app_id ', ' group_id '], ' unique ', ' targetattribute ' = [' app_id ', ' group_id '], ' message ' = ' app_id ' And group_id have been occupied! ‘],

integer: Integers

[' Age ', ' Integer '];

Number: Numeric

[' Salary ', ' number '];

Double: dual-precision floating-point

[' Salary ', ' double '];

Date: (dates)

[[' From ', ' to '], ' date ';

String: Strings

[' username ', ' string ', ' length ' = [4, 24]];

Boolean : Is a Boolean value | | Aliases for Cbooleanvalidator

[' Field name ', ' Boolean ', ' truevalue ' = ' = ', ' falsevalue ' = False, ' strict ' = true];

image : is a valid picture file

[' Primaryimage ', ' image ', ' extensions ' = ' png, jpg ', ' minWidth ' = ', ' maxWidth ' = +, ' m Inheight ' = +, ' maxheight ' + 1000]

Each : traversal, IDs, and Product_ids are collections of numbers

[' IDs ', ' product_ids '], ' each ', ' rule ' = [' integer ']],

Custom rules:

[' Password ',  ' ValidatePassword '],/** * validates the password. * this  method serves as the inline validation for password. * * @ param string  $attribute  the attribute currently being validated *   @param  array  $params  the additional name-value pairs given in  the rule */public function validatepassword ($attribute,  $params) {     if  (! $this->haserrors ())  {         $user   =  $this->getuser ();        if  (! $user  | |  ! $user->validatepassword ($this->password))  {              $this->adderror ($attribute,  ' account or password error! ');         }    }} 

Yii2 rules Validation rule

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.