You are viewing revision #1 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.
This is a reference to be used for Model rule validation. The documentation 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.
To be continued
Validation rules reference ¶
TODO: Write all references
Selected readings ¶
The Definitive Guide to Yii: Working with Forms - Creating Model
Yii PHP Framework Class Reference - CValidator
Built in validators in Yii ¶
CBooleanValidator
CCaptchaValidator
CCompareValidator
CDefaultValueValidator
CEmailValidator
CExistValidator
CFileValidator
CFilterValidator
CInlineValidator
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.