You are viewing revision #2 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version or see the changes made in this revision.
This is a reference to be used for Model rule validation 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
How validation works ¶
The CModel class uses a method named rules to return an array with the rules for validation.
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() 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:
- A method in the model class with the same name as the validator specified.
- A builtin validator in Yii that extends the CValidator class.
- A path/alias to a home made CValidator class that is not built in.
Scenarios ¶
TODO: Write shortly about scenarios.
Standard parameters ¶
array(
'attribute list',
'validator name',
'on'=>'scenario name',
'message'=>'The attribute didn\'t validate!',
...validation parameters...
);
attribute list
: specifies the attributes (separated by commas) to be validated;validator name
: specifies the validator to be used.on
: 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.message
: Error message if validation fails....validation parameters...
: any number of extra parameters to be used by the specified validator.
Validation rules reference ¶
boolean
: CBooleanValidatorcaptcha
: CCaptchaValidatorcompare
: CCompareValidatordefault
: CDefaultValueValidatoremail
: CEmailValidatorexists
: CExistValidatorfile
: CFileValidatorfilter
: CFilterValidatorin
: CRangeValidatorlength
: CStringValidatornumerical
: CNumberValidatormatch
: CRegularExpressionValidatorrequired
: CRequiredValidator, validates that the specified attribute does not have null or empty value.safe
: CSafeValidatortype
: CTypeValidatorunique
: CUniqueValidatorunsafe
: CUnsafeValidatorurl
: CUrlValidator
Selected readings ¶
The Definitive Guide to Yii: Working with Forms - Creating Model
CModel::validate()
CModel::rules()
CModel::$scenario
CValidator
CInlineValidator
Built in validators in Yii ¶
CBooleanValidator
CCaptchaValidator
CCompareValidator
CDefaultValueValidator
CEmailValidator
CExistValidator
CFileValidator
CFilterValidator
CNumberValidator
CRangeValidator
CRegularExpressionValidator
CRequiredValidator
CSafeValidator
CStringValidator
CTypeValidator
CUniqueValidator
CUnsafeValidator
CUrlValidator
compare operator default behavior
by default,
array('password', 'compare', 'on'=>'register')
compare with password_repeat. If you do not explicitly state what to compare. It will compare with comparedfield_repeat
CFilterValidator?
The example at the bottom is great but what we really need is a sample of each validator. Currently I am searching how exactly to use CFilterValidator
compare operator against avalue
Suppose you have a situation where you need to compare against a certain value. The most obvious case is yes/no situation.
You could use compare operator like this;
array('field_prop', 'compare', 'compareValue'=>'Y', 'message'=>'Your only choice is YES'),
Assuming that you have yes/no drop down and Y/N as values
File rule safe attribute
Hallo,
I found out that I have to set the safe rule attribute to true for the file rule.
array('myFile', 'file','safe'=>true, 'allowEmpty' => false, 'types'=>'pdf,docx', 'on' => 'myScenario'),
Why is it default set to false? Is there some explanation somewhere ?
Here is where it is said that its default is FALSE:
CFileValidator#safe-detail
thank you :)
Rule compare myDate with now()
How to compare myDate and now() with operator is '>=' ?
Rule compare myDate with now()
Hello nakovn, it is a little late but perhaps will be usefull for anyone
array('mydate','compare','compareValue'=>date('Y-m-d'),'operator'=>'>=')
custom date validation
HI i Have date in day(dob_days), month(dob_months) and year(dob_years) dropdowns and I want to validate date using yii. How can i do so? I am new to yii can anyone guide me? Also if is require custom rule, how can i take these 3 parameters into custom validation in model?
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.