Detailed introduction to Yii2.0rules verification rule set

Source: Internet
Author: User
Tags valid email address
This article mainly introduces the Yii2.0rules verification rule set, which has some reference value. if you are interested, you can refer to it. This article mainly introduces the collection of Yii2.0 rules verification rules, which has some reference value. if you are interested, you can refer to it.

I am also studying Yii2 recently. so today is a learning note!

Required: required value verification attribute

The code is as follows:

[['Field name'], required, 'requiredvalue '=> 'required value', 'message' => 'Prompt information']; # Description: alias of CRequiredValidator, ensure that the feature is not empty.

Email: email verification

The code is as follows:

['Email ', 'Email']; # Note: the alias of CEmailValidator ensures that the feature value is a valid email address.

Match: regular expression verification

[['Field name'], match, 'pattern' => 'regular expression', 'message' => 'Prompt information']; [['Field name'], match, 'Not '=> ture, 'pattern' => 'regular expression', 'message' => 'Prompt information'];/* reverse regex */# description: the alias CRegularExpressionValidator ensures that the feature matches a regular expression.

Url: url

The code is as follows:

['Website', 'URL', 'defaultscheme '=> 'http']; # Note: the alias of CUrlValidator ensures that the feature is a valid path.

Captcha: verification code

The code is as follows:

['Notificationcode', 'captcha ']; # Note: the alias of CCaptchaValidator ensures that the value of the feature is equal to the verification code displayed by captcha.

Safe: Secure

['description', 'safe'];

Compare: Comparison

The code is as follows:

['Age', 'compute', 'compute' => 30, 'operator' => '> =']; # Description: compareValue (constant value) -operator (comparison operator) # Description: the alias of CCompareValidator, which ensures that the feature value is equal to another feature or constant.

Default: default value

The code is as follows:

['Age', 'default', 'value' => null]; # Description: the alias of CDefaultValueValidator. a default value is assigned to the feature.

Exist: exist

The code is as follows:

['Username', 'exist']; # Description: the alias of CExistValidator. make sure that the attribute value exists in the specified data table field.

File: file

The code is as follows:

['Primaryimag', 'file', 'extension' => ['PNG ', 'jpg', 'GIF'], 'maxsize' => 1024*1024*1024]; # Note: the alias of CFileValidator ensures that the feature contains the name of an uploaded file.

Filter: filter

The code is as follows:

[['Username', 'Email '], 'filter', 'filter' => 'trim', 'skiponarray' => true]; # Description: alias of CFilterValidator, use a filter to convert attributes.

In: Range

The code is as follows:

['Level', 'in', 'range' => [1, 2, 3]; # Description: alias of CRangeValidator, make sure that the feature appears in a reserved value list.

Unique: Uniqueness

The code is as follows:

['Username', 'Unique'] # Note: the alias of CUniqueValidator ensures that the feature is unique in data table fields.

Integer: integer

['age', 'integer'];

Number: number

['salary', 'number'];

Double: double floating point type

['salary', 'double'];

Date: date

[['from', 'to'], 'date'];

String: string

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

Boolean: whether it is a boolean value.

The code is as follows:

['Field name', 'boolean', 'truevalue '=> true, 'falsevalue' => false, 'strict' => true]; # Description: alias of CBooleanValidator

Image: whether it is a valid image file

The code is as follows:

['primaryImage','image', 'extensions' => 'png, jpg,jpeg','minWidth' => 100,'maxWidth' => 1000,'minHeight' => 100,'maxHeight' => 1000,]

The above is a detailed description of the Yii2.0 rules verification rule set. For more information, see other related articles in the first PHP community!

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.