开发者

How to use only certain validation set for validating data in Cake PHP?

I was trying to validate my User model data and I came upon this problem.

Say i have the following validation rules, sto开发者_运维百科red in $validate variable:

var $validate=array(
        "username" => array(
            "usernameCheckForRegister" => array(
                "rule" => ..., 
                "message" => ...
            ),
            "usernameCheckForLogin" => array(
                "rule" => ...,
                "message" => ...
            )
        ),
        //rules for other fields
    );

In the UsersController controller, I have two actions: register() and login(). The problem is, how do I validate the username field in the register() action using ONLY the usernameCheckForRegister rule, and how do I validate the username field in the login() action, using the other rule, usernameCheckForLogin? Is there any behaviour or method in CakePHP which allows me to choose which set of rules to apply to a form field when validating?

Thank you in advance for your help!


I think I ran over the solution that fits my needs.

http://bakery.cakephp.org/articles/view/multivalidatablebehavior-using-many-validation-rulesets-per-model

Instead of defining several rules for each field, this behaviour implies defining several "general" rules under which you define all your field-related rules.

So, instead of doing:

var $validate=array(
    "username" => array(
        "usernameCheckForRegister" => array(
            "rule" => ..., 
            "message" => ...
        ),
        "usernameCheckForLogin" => array(
            "rule" => ...,
            "message" => ...
        )
    ),
    //rules for other fields
);

you do:

/**
 * Default validation ruleset
 */
var $validate = array(
        'username' => /* rules */,
        'password' => /* rules */,
        'email' => /* rules */
    );

/**
 * Custom validation rulesets
 */
var $validationSets = array(
    'register' => array(
        'username' => /* rules */,
        'password' => /* rules */,
        'email' => /* rules */,
    ),
    'login' => array(
        'username' => /* rules */,
        'password' => /* rules */
    )
); 

And then in your controller you toggle between validation sets like this:$this->User->setValidation('register');

Even though you have to write a little bit more code, I think this solution best fits my needs


Check the manual:

var $validate=array(
        "username" => array(
            "usernameCheckForRegister" => array(
                "rule" => ..., 
                "message" => ...,
                "on" => "create"
            ),
            "usernameCheckForLogin" => array(
                "rule" => ...,
                "message" => ...,
                "on" => "update"
            )
        ),
        //rules for other fields
    );

UPDATE: Oh... I just noticed that it seems impossible to use validation rule on login unless you update the user at each login attempt. You can modify login() method to verify the username instead.


Bit of a clunky solution but I've just been unsetting the ones I don't use from within the Controller. Could get messy but for a simple login/register it does the trick.

unset($this->User->validate['username']['usernameCheckForRegister']);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜