I had a validation challenge long time a go. The challenge was that I had to validate an attribute only if other attributes where of certain values or fulfilled some rules. For example, in order to validate for an attribute to be an email I needed to check if a dropdown parameter was sent with value 'email'.
One solution was to use beforeValidate and setup selection statements, but I thought that would be better to have a validator that could just plug set some rules and if they are passed without errors, then the important one was executed.
Download and extract on protected/components/validators (for the sake of the following example). For the sake of the example I am going to use one of the issues a fellow Yii'er had and requested this extension to help him out.
From its post and only the first and easiest requirement, for more advance example, please look at the post:
public function rules(){ return array( array( 'province', 'application.components.validators.EConditionalValidator', 'conditionalRules' => array('country', 'compare', 'compareValue' => 86), 'rule' => array('required') ), ); }
For enhancements, bug reports, etc. Please use the Forum Post
web development has never been so fun
www.2amigos.us
Total 3 comments
Just tried out your extension and it looks like it will solve my problem quite nicely. Thanks for you efforts!
I did notice one thing when I was trying to get it going and that was pass additional parameters to the "rule". My example is
I noticed that the format and allowEmpty parameters weren't being passed on to the validator.
in Function validateAttribute on line 80 there is an array_splice, which I think should be an array_slice? Otherwise we are removing the additional parameters and passing the name of the validator instead?
It seemed to solve my problem anyway.
For the validator to read correctly all the set attributes replace line 75
with
Useful extension, but it doesn't work if conditional rules are defined in the model and not in the database. Because you use getAttributes().
Leave a comment
Please login to leave your comment.