Conditional Validation

Rules are dynamically built from a database table. I’m trying to use thyseus extension (http://www.yiiframework.com/extension/yii-user-management) with lot of customization, so there is no a direct rule code.

Anyway, following the code I’m using now to dynamically build the rules:




if ($field->related_field_name)

{

     $parts = explode(';', $field->related_field_name);


     if (count($parts) == 1):

          $split = explode(':', $parts[0]);


          $validatingField = $split[0];

          $compareValue = $split[1];


          $rules[] = array(

              $field->varname,

              'EConditionalValidator',

              'conditionalRules' => array($validatingField, 'compare', 'compareValue'=>$compareValue),

              'rule' => array('required'),

          );

     else:                                


         $conditionalRules = array();

                           

         foreach ($parts as $part) {

             $split = explode(':', $part);


             $validatingField = $split[0];

             $compareValue = $split[1];




             $conditionalRules[] =

                 array(

                    $validatingField,

                    'compare',

                    'compareValue' => $compareValue,

                 );

         }

                                

         $rules[] = array(

             $field->varname,

             'EConditionalValidator',

             'conditionalRules' => array(

                 'group'=>$conditionalRules,

             ),

             'rule'=>array('required'),

         );

    endif;                            

}



In the future I will do a method to manage rules different from ‘required’, but at the moment I need only this kind of rule type. No time to do it now :D

Hey, great work. one question though, can this work on an array field? like if I want to validate for every element of $somearray[index][firstname] $somearray[index][lastname] ?

Thanks

Never tried, but my guess is that you will have to create a custom validator and then configure the conditional to work with it.

Hi can we use JQuery live validation for same?