你好我正在研究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(); ?>
答案 0 :(得分:0)
不要使用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);
}
答案 1 :(得分:-1)
为什么不在更新时将其删除?
像这样:
<?= $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,
]);
}