Difference between #1 and #2 of
Reference: Model rules validation

Revision #2 has been created by krillzip on Nov 29, 2009, 1:46:12 PM with the memo:

Added the "Validation rules reference" section, (temporarly saving)
« previous (#1) next (#3) »

Changes

Title unchanged

Reference: Model rules validation

Category unchanged

Tutorials

Yii version unchanged

Tags unchanged

Content changed

This is a reference to be used for Model rule validation. The documentation and is compiled from the Yii documentation and code. The purpose is to have all the information gathered in one place instead of scattered. This reference is not an intro.

Applys to Yii: 1.0.10
[...]
The [CModel](/doc/api/1.0.10/CModel) class uses a method named [rules](/doc/api/1.0.10/CModel#rules-detail) to return an array with the rules for validation.

To be continued
 
 
## Validation rules reference
 
 
TODO: Write all references
 
 
## Selected readings
 
[The Definitive Guide to Yii: Working with Forms - Creating Model](/doc/guide/form.model)<br />
 
[Yii PHP Framework Class Reference - C
 
```php 
public function rules()
 
{
 
    return array(
 
        array('username, password', 'required'),
 
        array('password_repeat', 'required', 'on'=>'register'),
 
        array('password', 'compare', 'on'=>'register'),
 
    );
 
}
 
```
 
 
The code above is an example of what the [rules()](/doc/api/CModel#rules-detail) function may look like. The arrays in the main array each one sets a rule of validation. 
 
 
### Choise of validators
 
 
Yii will look for validators in the specifik order:
 
 
1. A method in the model class with the same name as the validator specified.
 
2. A builtin validator in Yii that extends the CValidator class.
 
3. A path/alias to a home made  CValidator class that is not built in.
 
 
### Scenarios
 
 
TODO: Write shortly about scenarios.
 
 
### Standard parameters
 
 
 
```php 
array(
 
'attribute list', 
 
'validator name', 
 
'on'=>'scenario name', 
 
'message'=>'The attribute didn\'t validate!', 
 
...validation parameters...
 
);
 
```
 
 
+ <code>attribute list</code>: specifies the attributes (separated by commas) to be validated;
 
+ <code>validator name</code>: specifies the validator to be used.
 
+ <code>on</code>: this specifies the scenarios when the validation rule should be performed. Separate different scenarios with commas. If this option is not set, the rule will be applied in any scenario.
 
+ <code>message</code>: Error message if validation fails.
 
+ <code>...validation parameters...</code>: any number of extra parameters to be used by the specified validator.
 
 
## Validation rules reference
 
 
+ <code>boolean</code> : [CBooleanValidator](/doc/api/CBooleanValidator)
 
+ <code>captcha</code> : [CCaptchaValidator](/doc/api/CCaptchaValidator)
 
+ <code>compare</code> : [CCompareValidator](/doc/api/CCompareValidator)
 
+ <code>default</code> : [CDefaultValueValidator](/doc/api/CDefaultValueValidator)
 
+ <code>email</code> : [CEmailValidator](/doc/api/CEmailValidator)
 
+ <code>exists</code> : [CExistValidator](/doc/api/CExistValidator)
 
+ <code>file</code> : [CFileValidator](/doc/api/CFileValidator)
 
+ <code>filter</code> : [CFilterValidator](/doc/api/CFilterValidator)
 
+ <code>in</code> : [CRangeValidator](/doc/api/CRangeValidator)
 
+ <code>length</code> : [CStringValidator](/doc/api/CStringValidator)
 
+ <code>numerical</code> : [CNumberValidator](/doc/api/CNumberValidator)
 
+ <code>match</code> : [CRegularExpressionValidator](/doc/api/CRegularExpressionValidator)
 
+ <code>required</code> : [CRequiredValidator](/doc/api/CRequiredValidator), validates that the specified attribute does not have null or empty value.
 
+ <code>safe</code> : [CSafeValidator](/doc/api/CSafeValidator)
 
+ <code>type</code> : [CTypeValidator](/doc/api/CTypeValidator)
 
+ <code>unique</code> : [CUniqueValidator](/doc/api/CUniqueValidator)
 
+ <code>unsafe</code> : [CUnsafeValidator](/doc/api/CUnsafeValidator)
 
+ <code>url</code> : [CUrlValidator](/doc/api/CUrlValidator)
 
 
## Selected readings
 
[The Definitive Guide to Yii: Working with Forms - Creating Model](/doc/guide/form.model)<br />
 
[CModel::validate()](/doc/api/CModel#validate-detail)<br />
 
[CModel::rules()](/doc/api/CModel#rules-detail)<br />
 
[CModel::$scenario](/doc/api/CModel#scenario-detail)<br />
 
[CValidator](/doc/api/CValidator)<br />
 
[CInline
Validator](/doc/api/CInlineValidator)
 
### Built in validators in Yii
[CBooleanValidator](/doc/api/CBooleanValidator)<br />
[CCaptchaValidator](/doc/api/CCaptchaValidator)<br />
[...]
[CFileValidator](/doc/api/CFileValidator)<br />
[CFilterValidator](/doc/api/CFilterValidator)<br />
[CInlineValidator](/doc/api/CInlineValidator)<br />
 
[CNumberValidator](/doc/api/CNumberValidator)<br />
[CRangeValidator](/doc/api/CRangeValidator)<br />
[CRegularExpressionValidator](/doc/api/CRegularExpressionValidator)<br />
[CRequiredValidator](/doc/api/CRequiredValidator)<br />
[CSafeValidator](/doc/api/CSafeValidator)<br />
[CStringValidator](/doc/api/CStringValidator)<br />
[...]
99 0
114 followers
Viewed: 884 297 times
Version: 1.1
Category: How-tos
Written by: krillzip
Last updated by: Rodrigo
Created on: Nov 29, 2009
Last updated: 10 years ago
Update Article

Revisions

View all history