YII tutorial-how to use YiiCModel. rules ()

Source: Internet
Author: User
Tags valid email address
Declare the verification rule and rewrite this method. Each rule is an array with the following structure:

Public array rules ()
{Return}
Array indicates the validity rule applied when validate () is to be called.

Returns the validity rule of the attribute.

Declare the verification rule and rewrite this method. Each rule is an array with the following structure:

Array ('attribute list', 'validator name', 'on' => 'scenario name ',... Validation parameters ...)
Note:

Attribute list: specifies the attribute (separated by commas) for verification;
Validator name: specifies the verification program to be used. It can be the name of a built-in validators or validators class (or its path alias) of a model class of the method. One authentication method must have the following signature:
// $ Params refers to validation parameters given in the rulefunction validatorName ($ attribute, $ params)
A built-in validators are one of the validators declared in CValidator: builtInValidators. The validators class is the class that extends CValidator.
On: The condition specified by this option when the validity rule should be executed. Use commas to separate different methods. If this option is not set, the rule will be applied under any circumstances. For more details about this option, see.
Additional parameters are used to initialize the attributes of the validators. See the possible properties of the individal validators class API.

Here are some examples:
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 ),
);

Predefined complete list:

Boolean: the alias of CBooleanValidator. make sure that 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, ensuring that the value of a feature is equal to another feature or constant.

Email: the alias of CEmailValidator, which ensures that the feature value is a valid email address.

Default: the alias of cdefavaluvaluevalidator, 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, which ensures that the feature contains the name of an uploaded file.

Filter: the alias of CFilterValidator. a filter conversion attribute is used.

In: the alias of CRangeValidator, ensuring that the feature appears in a reserved value list.

Length: the alias of CStringValidator to ensure that the length of the feature 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. This ensures 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, which ensures that the feature is a valid path.

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.