2017-03-06 82 views
0

您好,我正在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(); ?> 
+0

首先你爲什麼顯示密碼有更好的移動密碼更新密碼部分並從更新頁面中刪除。 –

+0

是的,但它的客戶的要求。所以我需要在該頁面顯示密碼 – David

回答

0

爲什麼不在更新時刪除它?

像這樣:

<?= $model->isNewRecord ? $form->field($model, 'password_hash')->passwordInput(['maxlength' => true]) : "" ?> 

快樂編碼。 :)

UPDATE

如果密碼字段是真正需要的是有,那就試試這個在控制器:

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, 
    ]); 
} 
+0

感謝您的回答,但它是客戶的要求,所以我必須把它放在這個頁面上。 – David

+0

啊...然後我更新了答案。 – MegaGrindStone

+0

再次感謝,但我已經嘗試過,沒有發生任何事情。即使我在我的問題中提到它 – David

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]) ?> 

型號或的Controler例如

if ($this->validate()) { 
     $user = User::findOne($id); 
     $user->setPassword($this->password); 
     $user->generateAuthKey(); 
     $user->save(false); 
} 
+0

感謝兄弟,我嘗試過,但結果相同。 – David

+0

即使我改變字段名密碼password_hash但同樣的結果 – David

+0

如果我需要爲textInput而不是passwordInput那麼它的工作... – David

相關問題