0

我在从密码字段中删除值时遇到了麻烦。当我在未输入一个或多个必填字段的情况下提交注册表单时,它会在密码字段上生成一个 32 位字符值。

在这种情况下,我想看到这个字段是空白的。

感谢,

太太

4

3 回答 3

0

没有一些代码很难说,但听起来当表单验证在注册时失败时,表单会使用注册提交的数据重新填充(因为它是 CActiveForm) - 现在包括您散列的密码?

避免这种情况的一种方法是,在提交表单时,在 save() 或 validate() 失败后清除密码。我猜如果您有基本的 Yii 代码,您的表单发布操作将如下所示:

$model->attributes = $_POST['SignupModel'];
$model->password = cool_hash_function($model->password); // hashing the password - maybe you have this in beforeValidate(), I don't know
if ($model->save()) {
  $this->redirect('/success!'); // yay! redirect or whatever
} else {
  $model->password = ''; // if the save failed, clear out the password before re-rendering the form
}
$this->render('signup',array('model'=>$model)); // the form will now render without the pw

是的,用户需要重新输入密码。可能还有其他方法可以解决此问题,具体取决于您在保存之前对密码进行哈希处理的过程中的哪个位置 - 并且取决于我是否已经接近猜测您的实际问题。:)

于 2012-09-11T22:37:48.790 回答
0

您也可以在视图文件的代码中使用/方法的$htmlOptions参数,并强制密码输入呈现一个空值属性:CActiveFormCHtml

<?= CHtml::activePasswordField($model, 'password', array('value'=>'')) ?>
<?= $form->passwordField($model, 'password', array('value'=>'')) ?>
于 2012-10-14T21:50:47.527 回答
0

你的代码真的很糟糕,因为你从控制器进入模型功能,这违反了 MVC 编程模式的规则。

相反,这段代码应该放在用户模型类的beforeSave()方法中。

于 2013-01-24T15:48:24.117 回答