您好,我正在Yii2項目上工作。我有用戶模塊,在更新任何用戶的同時,輸入的密碼是用原來的密碼來的。我想在更新頁面上使密碼字段爲空。如何使更新頁面中的密碼字段爲空yii2
我正在使用密碼哈希,但在更新頁面密碼來與其原始值,我試圖null,提交但運氣不好。
我想:
<?= $form->field($model, 'password_hash')->passwordInput(['maxlength' => true,'value'=>null]) ?>
即使我嘗試 $model->password_hash=""
的控制器,但沒有發生。
但是沒有發生任何密碼字段與它的價值。
這是我的用戶模型規則:
public function rules() {
return [
[['first_name', 'last_name', 'address', 'mobile', 'email', 'password_hash', '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_hash'], '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_hash'],'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);
$roles= Roles::find()->all();
$model->password_hash="";
if ($model->load(Yii::$app->request->post())) {
$input=Yii::$app->request->post();
if($input['Users']['password_hash']!=""){
$model->password_hash=User::setPassword($model->password_hash);
}
//$model->auth_key=User::generateAuthKey();
$model->status=$input['Users']['status'];
unset($model->created_at);
$model->updated_at=date('Y-m-d H:i:s');
//echo "<pre>";print_r($model);exit;
$model->save();
Yii::$app->session->setFlash('success', "Record has been updated successfully !");
//return $this->redirect(['view', 'id' => $model->id]);
return $this->redirect(['index']);
} else {
return $this->render('update', [
'model' => $model,
'roles'=>$roles
]);
}
}
用戶表單:
<?php $form = ActiveForm::begin(); ?>
<div class="row">
<div class="col-md-4">
<?= $form->field($model, 'first_name')->textInput(['maxlength' => true]) ?>
</div>
<div class="col-md-4">
<?= $form->field($model, 'last_name')->textInput(['maxlength' => true]) ?>
</div>
</div>
<div class="row">
<div class="col-md-8">
<?= $form->field($model, 'address')->textarea(['rows' => 6]) ?>
</div>
</div>
<div class="row">
<div class="col-md-4">
<?= $form->field($model, 'mobile')->textInput(['maxlength' => true]) ?>
</div>
</div>
<div class="row">
<div class="col-md-4">
<?= $form->field($model, 'email')->textInput(['maxlength' => true]) ?>
</div>
<div class="col-md-4">
<?= $form->field($model, 'password_hash')->passwordInput(['maxlength' => true,'value'=>""]) ?>
</div>
</div>
<div class="row">
<div class="col-md-4">
<?= $form->field($model, 'status')->dropDownList(['0'=>'Active','1'=>'InActive']); ?>
</div>
<div class="col-md-4">
<?= $form->field($model, 'role_id')->dropDownList(ArrayHelper::map(Roles::find()->all(),'id','name')) ?>
</div>
</div>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => 'btn green']) ?>
<?= Html::a('Cancel', ['/ag-consumer'], ['class' => 'btn default']) ?>
</div>
<?php ActiveForm::end(); ?>
首先你爲什麼顯示密碼有更好的移動密碼更新密碼部分並從更新頁面中刪除。 –
是的,但它的客戶的要求。所以我需要在該頁面顯示密碼 – David