views:

89

answers:

3

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

Say i have the following validation rules, stored 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!

A: 

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.

bancer
Thanks for your answer. Still, I don't think that will be a clean solution. I was rather looking for something like $this->useValidationRule("usernameCheckForRegister");
linkyndy
+1  A: 

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

linkyndy
A: 

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']);
James F
The solution works, but I'd prefer something more flexible, as I'll be using many validation-rules sets.
linkyndy