phpyiiyii-validation

YiiFramework - Code redundancy on model rule declaration


would like to ask if is there a way that I can group multiple rules in the model into 1 so that i can reduce the amount of codes especially on places where same attribute needed to be check on multiple rules... Below is how the rules declaration now...

            array (
                    'new_email_addr',
                    'email',
                    'on' => 'chngEmail'
            ),
            array (
                    'new_email_addr',
                    'required',
                    'on' => 'chngEmail'
            ),
            array (
                    'new_email_addr',
                    'sameEmail',
                    'on' => 'chngEmail'
            ),

As you can see, the attribute new_email_addr needs to be check on 3 rules before it is save... What I wanted to achieve is something like this so i can do everything in one section.

            array (
                    'new_email_addr',
                    'required, email, sameEmail',
                    'on' => 'chngEmail'
            ),

Is there such possibilities?


Solution

  • No, that is not possible. You can group several fields, scenarios, and exceptions together but not several validators (see the definite guide for examples).

    The only way out were to create your custom validator combining the wanted ones as Samuel suggested.