Note: This version (1.0.0) is not compatible with earlier versions.
YiiConditionalValidator (YCV) validates some attributes depending on certains conditions (rules). You can use any core validator as you usually would do or any other class based or inline validator. An interesting feature is that you can use dot.notation in your rules to achieve data in related models and you can even use the own YiiConditionalValidator inside itself to perform more complex conditions;
Basically, YCV executes the rules set in the param if and if there are no errors executes the rules set in the param then.
Info: Please, visit the project's website and don't hesitate to fork it on github!
array('safeAttribsList', 'path.to.YiiConditionalValidator', 'if' => array( //rule1: array('attrX, attrY', 'required', ...) //ruleN: ... ) 'then' => array( //rule1: array('attrZ, attrG', 'required', ...) //ruleN: ... ) )
safeAttribsList: The name of the attributes that should be turned safe (since Yii has no way to make dinamic validators to turn attributes safe);path.to.YiiConditionalValidator: In the most of cases will be ext.YiiConditionalValidator;if: (bidimensional array) The conditional rules to be validated. Only if they are all valid (i.e., have no errors) then the rules in then will be validated;then: (bidimensional array) The rules that will be validated only if there are no errors in rules of if param;Note: Errors in the rules set in the param
ifare discarded after checking. Only errors in the rules set in paramthenare really kept.
If customer_type is "active" then birthdate and city are required:
public function rules() { return array( array('customer_type', 'ext.YiiConditionalValidator', 'if' => array( array('customer_type', 'compare', 'compareValue'=>"active"), ), 'then' => array( array('birthdate, city', 'required'), ), ), ); }
If customer_type is "inactive" then birthdate and city are required and city must be "sao_paulo", "sumare" or "jacarezinho":
public function rules() { return array( array('customer_type', 'ext.YiiConditionalValidator', 'if' => array( array('customer_type', 'compare', 'compareValue'=>"active"), ), 'then' => array( array('birthdate, city', 'required'), array('city', 'in', 'range' => array("sao_paulo", "sumare", "jacarezinho")), ), ), ); }
If information starts with 'http://' and has at least 24 chars length then the own information must be a valid url:
public function rules() { return array( array('information', 'ext.YiiConditionalValidator', 'if' => array( array('information', 'match', 'pattern'=>'/^http:\/\//'), array('information', 'length', 'min'=>24, 'allowEmpty'=>false), ), 'then' => array( array('information', 'url'), ), ), ); }
Note: This feature may not fit into situations too much complex.
You can use dot.notation in attribute name to fetch data from a related model in your rules.
Example:
Assuming that Customer has a relation 'profile', you could check (in customer rules) if the profile.username is not empty before validate something:
//Customer Model public function rules() { return array( array('information', 'ext.YiiConditionalValidator', 'if' => array( //would only return true if profile.username is not empty array('profile.username', 'required'), ), 'then' => array( array('someAttrib', 'someValidation', ...), ), ), ); }
[Version 1.0.0]
if/then operators replace validations/dependentValidations making the use more natural;[Version 0.2.0]
[ToDo]
Total 9 comments
Hi Sidtj,
i disabled the client side validation and it works! Thanks!
Hi Neil Bardos,
YCV may not work for client-side validation for now. Try to use server-side validation.
Hope it works.
Downloaded the extension and tried implementing it using this code. Did I missed something? I am new to Yii and any help would be greatly appreciated.
Note: cortisonDosage text field is required once cortisonUsed value is 1 [Yes].
The native Form Validation works but it doesnt displays error messages when cortisonDosage is empty.
Thanks in advance guys!
I also need the help for client side validations for elements which are enabled on any event of another elements.....
Very good work, and thank you to developer for precious support!!
i tried with other field but is the same thing, nothing work. No error message in log either in browser console. What can i send you to help me to solve the problem?
following my validation rule
Try to make different validations using another fields and rules for testing. Let me know the problems you may have. Everything is working in all of projects I use YCV in.
I followed all the steps, but nothing is working. I try with a simple test.
if 'vendi_a' is equal to 'privati' then 'prezzo_privati' must be insert. When prezzo_privati is empty no error message appears. I use the last version of yii, is this the problem?
Hi, is it possible to create client side conditional validation like you described for backend?
Leave a comment
Please login to leave your comment.