有一些验证问题,只有在使用Auth组件时才会出现。
我的注册表格中有4个字段:username
,password
,password_confirm
和email
。
我的用户模型附加了多变量行为。以下是适用于登记表格的规则:
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(
)*/
)
这是我的注册表格:
<?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));?>
现在,每次我提交表单EMPTY时,密码字段虽然为空,但会通过所有验证测试(我尝试将value => ''
放在代码中,但它没用)。
此外,电子邮件输入似乎通过了'notEmpty'
测试,显示的错误是The email is not valid
我查看了所有代码,但找不到任何解决方案。
答案 0 :(得分:0)
这就是我要用的。
答案 1 :(得分:0)
关于密码问题:您使用的是哪个版本的CakePHP?
1.2中的Auth组件会自动对您的密码进行哈希处理,这样即使是密码也不会为空。在1.3中它应该没问题,但我不知道从哪个版本。
答案 2 :(得分:0)
我设法做了几次“黑客攻击”,所以问题现在已经解决了。 不要认为这是最合适的方式,但对于遇到我问题的其他用户来说它可能会派上用场:
首先,在我的UsersController中,我编写了此代码段,因此如果用户将密码字段留空,请在验证前将其重置为''
:
if($this->data['User']['password'] == $this->Auth->password('')){
$this->data['User']['password'] = '';
}
$this->User->set($this->data);
if($this->User->validates()){ //post validation login }
第二个问题是电子邮件验证。奇怪的是,我通过更改 Multivalidatable Behavior中规则的顺序来解决此问题。所以,来自:
"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!"
)
)
我现在有:
"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!"
)
)
我不知道这是否有意,但似乎未完成的最后规则是显示的规则。根据我的逻辑,规则将按其出现顺序排列,因此如果第一个不适用,请停止检查并显示它。
我再说一遍,我不知道这是不是正确的做法,但我似乎已经解决了这些问题。
如果你有什么要加的话,请这样做!
修改强> 在Cake 1.3 Book中找到了“官方”的解释。它说:
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.
因此,我的问题的另一种方法是按照它们的出现顺序设置验证规则,并在规则数组中添加last
键。