There might be a special case where an attribute is a single-dimension array. In case you want to validate all of the array elements with a single validator you could use this extension Actually it is a validator which calls another validator in a loop.
Simple usage is like this:
array( 'arrayAttibute', 'ext.validators.ArrayValidator', 'validator' => 'length', //or 'ext.validators.MyCustomValidator' 'params' => array( 'max' => 3, ), 'separateParams' => false, 'allowEmpty' => false, ),
In case you want a somehow different treatment for each array element you can use a more complex version of the above code where you define the key of each element and you must specify separate parameters for each key:
array( 'arrayAttibute', 'ext.validators.ArrayValidator', 'validator' => 'exist', 'params' => array( 'key1' => array( 'allowEmpty' => false, 'attributeName' => 'municipality_name', 'className' => 'Municipality', ), 'key2' => array( 'allowEmpty' => false, 'attributeName' => 'county_name', 'className' => 'County', ), ), 'separateParams' => true, ),
The ArrayValidator class just happens to be located inside extensions/validators folder. Of course you could place it anywhere you have other custom validators
Total 10 comments
Please fix Yii::t calls to contain a message category, i.e. Yii::t('ArrayValidator','message');
Correct, I think params should be initialized as $params = array() in the class. Also, I never really used the separateParams bit. ArrayValidator v2.1? :)
Obviously there is a mistake, if you leave params as wells as separateParams it will fail, since the foreach starts without checking params for NULL. If you initialize params as array, the problem disappears.
No problem, it's a very useful extension :)
Thanks marcovtwout since he did a nice refactoring of my code and placed all the necessary comments in the right place. But still he left all the functionality intact. The syntax is slightly different since you now can use the names of the validators. 'range' instead of 'CRangeValidator' I kept his name as a coauthor of this extension :) Now you can just download the new version 'ArrayValidator_v2.zip' from this page on the right and not have to visit another page.
Here is my version of the ArrayValidator: http://pastebin.com/xgWyRs7q
Declaration of ArrayValidator::isEmpty() should be compatible with that of CValidator::isEmpty().
Also, please consider using proper PHPDoc for your class, functions and properties.
the is empty function is now included as part of the class so the code is more self sufficient. The original empty() function of php considers zero(0) as empty and this has caused me lots of troubles in the past. That's why I recommend use a different version that suits you best :)
Can you please explain or show how to use it. Because i have to validate fields for which i have taken array. And as i am new to yii i dont know how to do that...
I tried using this extension in my project, but its giving me this error include(CHtmlEx.php) [function.include]: failed to open stream: No such file or directory ...
Can u please help me solve this. Thanks
Leave a comment
Please login to leave your comment.