Yii CModel. rules () method, a complete list of predefined values of validate, and verification

Source: Internet
Author: User
Tags valid email address yii
Public arrayRules()
{Return} Array The validity rule applied when you want to call validate.

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 rule
    function 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: 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.

 

Verification implementation:

In fact, the combination of CModel. rules () + CActiveForm. validate implements the verification function.
Example:
[View]
<Div class = "form">
<? Php $ form = $ this-> beginWidget ('cactiveform');?>

<? Php echo $ form-> errorSummary ($ model);?> // Note 1: the error message is displayed here.
......
<? PHP $ this-> endwidget ();?>
</Div>

[Control]
$ Model = new user;
If (isset ($ _ post ['user'])
{
$ Model-> attributes = $ _ post ['user'];
If ($ model-> validate () & $ model-> Save () // NOTE 2: $ model-> validate () indicates that model. Rules is called for verification.
$ This-> redirect (Array ('view', 'id' => $ model-> ID ));
}

[Model]
Class user extends cactiverecord
......
Public Function rules ()
{
// Note: You shocould only define rules for those attributes that
// Will receive user inputs.
Return array (
Array ('username, password, email ', 'required '),
Array ('username, password, email ', 'length', 'max' => 128 ),
Array ('Id, username, password, email ', 'safe', 'on' => 'search '),
);
}
......

To implement more powerful and complex verification functions, we will also post in rules.
For Ajax verification, we will introduce it in the Ajax topic. Of course, it will be added when necessary.

========================================================== ======

Embellishment entertainment

========================================================== ======

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.