Yii 1.1: array-validator

Repeat a (custom or embedded) validator for an array attribute
14 followers

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.

Usage ΒΆ

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 14 comments

#18205 report it
junaidatari at 2014/09/25 04:58pm
suggestion

Try to add min and max for array items then it will be perfect.

#15340 report it
spatan at 2013/10/30 07:38am
Your extension lack proper documentation

You have done a great job which would help alot of us but the documentation is poor.

What happens in the controller and in the view ?

How do i call the $model->validate(), or do i have to loop.

What happens in the view? how do i render the array attributes?

Please kindly reply to this.

#15339 report it
spatan at 2013/10/30 07:38am
Your extension lack proper documentation

You have done a great job which would help alot of us but the documentation is poor.

What happens in the controller and in the view ?

How do i call the $model->validate(), or do i have to loop.

What happens in the view? how do i render the array attributes?

Please kindly reply to this.

#13951 report it
spatan at 2013/07/09 06:01am
What kind of extension is this without proper documentation

Please could you explain how to use your extension in a controller and in the view.

The model rules explanation above seem straight forward. What about in view and controller, how do i use it?

#10582 report it
Maxxer at 2012/11/06 01:34am
Missing translation category

Please fix Yii::t calls to contain a message category, i.e. Yii::t('ArrayValidator','message');

#10347 report it
marcovtwout at 2012/10/22 04:00am
Re: Problems with leaving params

Correct, I think params should be initialized as $params = array() in the class. Also, I never really used the separateParams bit. ArrayValidator v2.1? :)

#10310 report it
yiibie at 2012/10/18 02:40pm
Problems with leaving params

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.

#7941 report it
marcovtwout at 2012/04/27 10:27am
Re: When the community works! :)

No problem, it's a very useful extension :)

#7938 report it
pligor at 2012/04/27 08:17am
When the community works! :)

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.

#7162 report it
marcovtwout at 2012/02/29 05:12am
Some improvements

Here is my version of the ArrayValidator: http://pastebin.com/xgWyRs7q

  • supports custom validators as well as build-in and inline validators
  • modifies original attribute in array, so you can also use it for filters (CFilterValidator)
  • uses isEmpty() from parent class like all other Yii validators
  • properly documented
  • slighly changed syntax
#7151 report it
marcovtwout at 2012/02/28 07:33am
Strict error

Declaration of ArrayValidator::isEmpty() should be compatible with that of CValidator::isEmpty().

Also, please consider using proper PHPDoc for your class, functions and properties.

#6497 report it
pligor at 2012/01/14 03:31pm
Ok

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 :)

#6362 report it
gans at 2012/01/03 08:47am
Can you please explain in detail ???

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...

#6290 report it
richa at 2011/12/26 12:22am
CHtmlEx.php error

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 to leave your comment.

Create extension