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
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
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...
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 :)
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.
Some improvements
Here is my version of the ArrayValidator: http://pastebin.com/xgWyRs7q
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.
Re: When the community works! :)
No problem, it's a very useful extension :)
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.
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? :)
Missing translation category
Please fix Yii::t calls to contain a message category, i.e. Yii::t('ArrayValidator','message');
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?
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.
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.
suggestion
Try to add min and max for array items then it will be perfect.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.