[英]How to make password field null in update page yii2
您好,我正在從事Yii2項目。 我有用戶模塊,在更新任何用戶時,輸入的密碼與原始密碼一起出現。 我想在更新頁面上使密碼字段為空。
我正在使用密碼哈希,但是在更新頁面密碼及其原始值中,我試圖將所提交的內容設為空,但運氣不好。
我試過了 :
<?= $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(); ?>
不要使用password_hash。 您必須在模型中創建新變量,例如密碼
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);
}
為什么不將其全部刪除呢?
像這樣:
<?= $model->isNewRecord ? $form->field($model, 'password_hash')->passwordInput(['maxlength' => true]) : "" ?>
快樂的編碼。 :)
如果確實需要密碼字段,請在Controller中嘗試:
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,
]);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.