Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在更新页面yii2中将密码字段设为空_Php_Forms_Yii2 - Fatal编程技术网

Php 如何在更新页面yii2中将密码字段设为空

Php 如何在更新页面yii2中将密码字段设为空,php,forms,yii2,Php,Forms,Yii2,你好,我在Yii2项目工作。我有一个用户模块,在更新任何用户时,输入的文件密码将与原始密码一起提供。我想在更新页面上设置密码字段为空 我使用的是密码散列,但在更新页面密码的原始值中,我试图将该字段设为空,但运气不好 我试过: <?= $form->field($model, 'password_hash')->passwordInput(['maxlength' => true,'value'=>null]) ?> 用户控制器: public $passwo

你好,我在Yii2项目工作。我有一个用户模块,在更新任何用户时,输入的文件密码将与原始密码一起提供。我想在更新页面上设置密码字段为空

我使用的是密码散列,但在更新页面密码的原始值中,我试图将该字段设为空,但运气不好

我试过:

<?= $form->field($model, 'password_hash')->passwordInput(['maxlength' => true,'value'=>null]) ?>
用户控制器:

public $password;
public function rules() {
            return [
                [['first_name', 'last_name', 'address', 'mobile', 'email', 'password', 'role_id'], 'required'],
                [['address'], 'string'],
                [['role_id'], 'integer'],
                [['email'], 'email'],
                [['email'], 'unique', 'targetAttribute' => ['email']],
                [['created_at', 'updated_at'], 'safe'],
                [['first_name', 'last_name', 'email', 'password'], 'string', 'max' => 255],
                [['mobile'], 'required','on'=>'create,update'],
                //[['mobile'], 'string','max'=>10],
                [['mobile'], 'number','numberPattern' => '/^[0-9]{10}$/','message'=>"Mobile must be integer and should not greater then 10 digit"],
                [['password'],'string','min'=>6],
                //[['mobile'], 'number'],
                [['status'], 'string', 'max' => 1,'on'=>'create,update'],
                [['role_id'], 'exist', 'skipOnError' => true, 'targetClass' => Roles::className(), 'targetAttribute' => ['role_id' => 'id']],
            ];
        }
用户表格:


为什么不在更新时删除它呢

像这样:

public function actionUpdate($id)
{
    $model = $this->findModel($id);
    $password = $model->password_hash; // backup the value first;
    $model->password_hash = "";

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        $model->password_hash = $password; // retreive the value back
        $model->save();
        // redirect here
    }
    return $this->render('update', [
        'model' => $model,
    ]);
}

不要接受密码散列。您必须在模型中创建新变量,例如密码

<?= $form->field($model, 'password')->passwordInput(['maxlength' => true,'value'=>null]) ?>
看法


首先,为什么要在那里显示密码?最好将密码移到更新密码部分,并从更新页面中删除。是的,但这是客户的要求。所以我需要在那个页面显示密码谢谢回答,但这是客户的要求,所以我必须把它放在这个页面上。啊。。。然后更新了我的答案。再次感谢,但我已经尝试过了,什么也没发生。甚至我在信中也提到过question@deep我猜你错过了一些东西,否则这个答案会有用的,试着一个接一个地调试。嗯。。。我猜你的用户模型规则有问题。想和大家分享一下吗,@DeepParekh?即使我把域名密码改成了密码\u散列,但结果还是一样这是唯一的实践例子
<?= $model->isNewRecord ? $form->field($model, 'password_hash')->passwordInput(['maxlength' => true]) : "" ?>
public function actionUpdate($id)
{
    $model = $this->findModel($id);
    $password = $model->password_hash; // backup the value first;
    $model->password_hash = "";

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        $model->password_hash = $password; // retreive the value back
        $model->save();
        // redirect here
    }
    return $this->render('update', [
        'model' => $model,
    ]);
}
public $password;
public function rules() {
            return [
                [['first_name', 'last_name', 'address', 'mobile', 'email', 'password', 'role_id'], 'required'],
                [['address'], 'string'],
                [['role_id'], 'integer'],
                [['email'], 'email'],
                [['email'], 'unique', 'targetAttribute' => ['email']],
                [['created_at', 'updated_at'], 'safe'],
                [['first_name', 'last_name', 'email', 'password'], 'string', 'max' => 255],
                [['mobile'], 'required','on'=>'create,update'],
                //[['mobile'], 'string','max'=>10],
                [['mobile'], 'number','numberPattern' => '/^[0-9]{10}$/','message'=>"Mobile must be integer and should not greater then 10 digit"],
                [['password'],'string','min'=>6],
                //[['mobile'], 'number'],
                [['status'], 'string', 'max' => 1,'on'=>'create,update'],
                [['role_id'], 'exist', 'skipOnError' => true, 'targetClass' => Roles::className(), 'targetAttribute' => ['role_id' => 'id']],
            ];
        }
<?= $form->field($model, 'password')->passwordInput(['maxlength' => true,'value'=>null]) ?>
 if ($this->validate()) {
        $user = User::findOne($id);
        $user->setPassword($this->password);
        $user->generateAuthKey();
        $user->save(false);
}