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

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

12 0
15 followers
1 388 downloads
Yii Version: 1.1
License: BSD-2-Clause
Category: Validation
Developed by: pligor
Created on: Nov 29, 2011
Last updated: 8 years ago

Downloads

show all

Related Extensions