【发布时间】:2011-05-09 20:58:22
【问题描述】:
有些验证问题似乎只在使用 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
我查看了所有代码,但找不到任何解决方案。
【问题讨论】:
-
你是如何相互核对密码的?
-
与 auth 一起使用的密码字段永远不会为空,您要做的就是检查 compare_pw 字段是否有效以及哈希版本是否与密码字段匹配。你说你的解决方案是一个黑客是正确的。你可以比较然后像 return Security::hash($this->data['User']['compare'], null, true) === $this->data['User']['password'];
-
这就是我所做的。实际上,我的问题出在控制器内部,即如何“重置”密码字段。我在下面的“编辑”中弄清楚了。但是我没有使用 Security::hash(),而是使用 $this->Auth->password() 来与 Auth 组件保持一致... :) 另外,一个重要的注意事项是
password字段将仅当您在表单中还有username字段时才进行散列。否则它将保持纯文本。
标签: authentication cakephp validation