[Validator] Switchcompare Validator Use For Conditional Model Rule

Hi Guys,

Yesterday I made this SwitchCompareValidator to used it for conditional rule in model.

SwitchCompareValidator is based on YiiConditionalValidator with several changes including cactiveform clientValidation support.

Yes, It support cactiveform clientValidation.

Here is example of usage :




array('periode,title','ext.MyValidators.SwitchCompareValidator',

'switch'=>'type', //switch(type)

'cases'=>array(

    array( //case 'NEW'

	'compare'=>array(

		'compareValue'=>'NEW'

	),

	'then'=>array(

		array('periode','default',value=>date('Y-m-d',now()),setOnEmpty=>false)

	)

    ),

    array( // case type in array('OLD','EXPIRED')

	'compare'=>array(

		'operator'=>'in',

		'compareValue'=>array('OLD','EXPIRED')

	),

	'then'=>array(

		array('periode','required'),

	)

    ),

    array(   // default

	'then'=>array(

		array('periode','in','range'=>array('DAILY','MONTHLY','WEEKLY','ALWAYS'),'allowEmpty'=>false),

		array('title','required')

	)

    )

)),



Explanatioin :

SwitchCompareValidator

Compares the specified attribute value with another value and if they are equal, run validation in "then" seqment.

[list=1]

[*] switch, the name of attribute to be compare with. could also be a value. usage: ‘switch’=>attribute or ‘switch’=>array(‘attribute’=>attribute) or ‘switch’=>array(‘value’=>value)

[*] cases, array of array of case. each case contain ‘compare’ and ‘then’. at least there are one case with ‘compare’. only last case could omit ‘compare’ (serve as default).

[/list]

case[‘compare’], Compares the specified attribute value with another value.

[list=1]

[*] compareAttribute, the name of the attribute to be compared with "switch".

[*] compareValue, the constant value to be compared with "switch".

[*] operator, the operator for comparison. (default:"=") (supported: =,==,>,>=,<,<=,in,notin)

[*] strict, whether the comparison is strict (both value and type must be the same). (default:false)

[/list]

case[‘then’], array of validation to be test if case[‘compare’] is true. (or inside default). each validation format:

[list=1]

[*] array(attribute,validator,param). (you may used dot.notation in your rules to achieve data in related models. attribute must be one of the attribute in SwitchCompareValidator rule) or

[*] array(validator,param). (if attribute omited, it will use all attribute specified in SwitchCompareValidator rule)

[/list]

Download 5230

SwitchCompareValidator.php
.

Thanks.

  • note: dot.notation NOT support CActiveform clientValidation and still NOT tested, hope someone could do this for me.

  • note: if there is a bug, please post it here.

  • note: if there is some improvement you could think or solve of don’t hesitate to point it out and post it here.

  • note: +1 if you found this usefull.