简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM