开发者

CakePHP Auth Component validation issue

Have some validating problems which seem to appear only when using the Auth component. I have 4 fields in my register form: username, password, password_confirm and email.

I also have the multivalidatable behaviour attached to my User model. Here are the rules which apply to the register form:

var $validationSets=array(
    "register" => array(
        "username" => array(
            "usernameFieldNotEmpty" => array(
                "rule" => "notEmpty",
                "message" => "You did not enter the username!"
            ),
            "usernameValid" => array(
                "rule" => "__alphaNumericDashUnderscore",
                "message" => "The username you entered is not valid!"
            ),
            "usernameExistsInDatabase" => array(
                "rule" => array("__existingRecord", false), 
                "message" => "The username you entered has been already registered in our database!"
            )
        ),
        "password" => array(
            "passwordFieldNotEmpty" => array(
                "rule" => "notEmpty",
                "message" => "You did not enter your password!"
            )
        ),
        "password_confirm" => array(
            "passwordConfirmFieldNotEmpty" => array(
                "rule" => "notEmpty",
                "message" => "You did not confirm your password!"
            ),
            "passwordsMatch" => array(
                "rule" => array("__fieldMatch", "password"),
                "message" => "The passwords you entered don't match!"
            )
        ),
        "email" => array(
            "emailFieldNotEmpty" => array(
                "rule" => "notEmpty",
                "message" => "You did not enter the e-mail!"
            ),
            "emailValid" => array(
                "rule" => "email",
                "message" => "The e-mail you entered is not valid!"
            ),
            "emailExistsInDatabase" => array(
                "rule" => array("__existingRecord", false), 
                "message" => "The e-mail you entered has been already registered in our database!"
            )
        )
        /*"language" => array(

        )*/
    )

Here is my register form:

<?php echo $this->Form->create('User', array('url' => array('controller' => 'users', 'action' => 'register')))开发者_开发技巧;?>
<fieldset>
    <legend><?php __('Add User'); ?></legend>
<?php
    echo $this->Form->input('username');
    echo $this->Form->input('password', array('type' => 'password', 'value' => ''));//value='' - resets the password input on any error on the page
    echo $this->Form->input('password_confirm', array('type' => 'password', 'value' => ''));
    echo $this->Form->input('email');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit', true));?>

Now, everytime I submit the form EMPTY, the password field, although empty, passes all validation tests (I tried putting value => '' in the code, but it's useless). Also, the email input seems to pass the 'notEmpty' test and the error shown is that The email is not valid

I've looked over all my code but couldn't find any solution.


http://pastebin.com/xnQ02BCW

this is what i would use.


About the Password problem: what version of CakePHP are you using?

The Auth component in 1.2 automatically hash your password, so that it will result not empty even if it is. In 1.3 it should be OK but I don't know from what version.


I've managed to do a couple of "hacks" so the problem is solved for now. Don't think this is the most appropriate way of doing it, but it might get in handy for other users having my problem:

Firstly, in my UsersController, I wrote this snippet, so if the password field is left empty by the user, reset it to '' before validation:

if($this->data['User']['password'] == $this->Auth->password('')){
            $this->data['User']['password'] = '';
        }
        $this->User->set($this->data);
        if($this->User->validates()){ //post validation login }

The second problem was the e-mail validation. Oddly, I got this problem fixed by changing the order of the rules in the Multivalidatable Behaviour. So, from:

"email" => array(
        "emailFieldNotEmpty" => array(
            "rule" => "notEmpty",
            "message" => "You did not enter the e-mail!"
        ),
        "emailValid" => array(
            "rule" => "email",
            "message" => "The e-mail you entered is not valid!"
        ),
        "emailExistsInDatabase" => array(
            "rule" => array("__existingRecord", false), 
            "message" => "The e-mail you entered has been already registered in our database!"
        )
    )

I now have:

"email" => array(
            "emailExistsInDatabase" => array(
                "rule" => array("__existingRecord", false), 
                "message" => "The e-mail you entered has been already registered in our database!"
            ),
            "emailValid" => array(
                "rule" => "email",
                "message" => "The e-mail you entered is not valid!"
            ),
            "emailFieldNotEmpty" => array(
                "rule" => "notEmpty",
                "message" => "You did not enter the e-mail!"
            )
        )

I don't know whether this was intended or not, but it seems like the last rule which isn't fulfilled is the one displayed. To my logic, the rules would have been arranged in their order of appearance, so if the first one doesn't apply, stop checking and display it.

I repeat, I don't know if this is the right approach, but I seem to have worked out these problems.

If you have anything to add up, please do so!

EDIT Found the 'official' explanation in the Cake 1.3 Book. It says:

By default CakePHP tries to validate a field using all the validation rules declared for it and returns the error message for the last failing rule. But if the key last is set to true for a rule and it fails, then the error message for that rule is returned and further rules are not validated. So if you prefer to show the error message for the first failing rule then set 'last' => true for each rule.

So, another approach to my problem is to set the validation rules in their order of appearance and add a last key to the rule array.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜