提交具有必填字段的注册表单空白然后在密码字段上生成32位字符值

时间:2012-09-11 11:14:55

标签: yii

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

我想在这种情况下将此字段视为空白。

谢谢,

MRS

3 个答案:

答案 0 :(得分: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

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

答案 1 :(得分:0)

您也可以使用$htmlOptions / CActiveForm方法的CHtml参数在视图文件中执行此操作,并强制输入密码以呈现空值属性:< / p>

<?= CHtml::activePasswordField($model, 'password', array('value'=>'')) ?>
<?= $form->passwordField($model, 'password', array('value'=>'')) ?>

答案 2 :(得分:0)

您的代码非常糟糕,因为您正在从Controller进入模型功能,这违反了MVC编程模式的规则。

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