2016-05-17 1 views
1

パスワード変更機能を使用しようとしているときに、ユーザーが入力するフォームを作成して、ハッシュしてpassword_hashレコードに追加します。以前のパスワードが入力されないように、私は'value' => ''を追加しました。Yii2 - 空白のままにしておくと古いデータを取るためのアクティブフォームを作成する

しかし、私はこの問題を抱えています:フィールドをデフォルトの状態のように空白にしておくと、データベースのpassword_hashフィールドも空白になります。このフィールドが空白の場合、データベース内の古い値が自動的に取り込まれ、データが入力されている場合は代わりにその値が使用されるという例外をどうやって作ることができますか?

<?= $form->field($model, 'password')->passwordInput(['value'=>'']) ?> 

EDIT:

<?php 

      namespace app\controllers; 

      use app\models\User; 
      use Yii; 
      use app\models\UserList; 
      use app\models\UserlistSearch; 
      use yii\web\Controller; 
      use yii\web\NotFoundHttpException; 
      use yii\filters\VerbFilter; 

      /** 
      * UserlistController implements the CRUD actions for UserList model. 
      */ 
      class UserlistController extends Controller 
      { 
       /** 
       * @inheritdoc 
       */ 
       public function behaviors() 
       { 
        return [ 
         'verbs' => [ 
          'class' => VerbFilter::className(), 
          'actions' => [ 
           'delete' => ['POST'], 
          ], 
         ], 
        ]; 
       } 

       /** 
       * Lists all UserList models. 
       * @return mixed 
       */ 
       public function actionIndex() 
       { 
        $searchModel = new UserlistSearch(); 
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams); 

        return $this->render('index', [ 
         'searchModel' => $searchModel, 
         'dataProvider' => $dataProvider, 
        ]); 
       } 

       /** 
       * Displays a single UserList model. 
       * @param integer $id 
       * @return mixed 
       */ 
       public function actionView($id) 
       { 
        return $this->render('view', [ 
         'model' => $this->findModel($id), 
        ]); 

       } 
       public function actionDomains($id) 
       { 
        return $this->render('domains', [ 
         'model' => $this->findModel($id), 
        ]); 

       } 
       /** 
       * Creates a new UserList model. 
       * If creation is successful, the browser will be redirected to the 'view' page. 
       * @return mixed 
       */ 
       public function actionCreate() 
       { 
        $model = new UserList(); 

        if ($model->load(Yii::$app->request->post()) && $model->save()) { 
         return $this->redirect(['view', 'id' => $model->id]); 
        } else { 
         return $this->render('create', [ 
          'model' => $model, 
         ]); 
        } 
       } 

       /** 
       * Updates an existing UserList model. 
       * If update is successful, the browser will be redirected to the 'view' page. 
       * @param integer $id 
       * @return mixed 
       */ 


       public function actionUpdate($id) 
       { 
        $model = $this->findModel($id); 

        if ($model->load(Yii::$app->request->post())) { 
         $model->password_hash=Yii::$app->getSecurity()->generatePasswordHash($model->password_hash); 
         $model->save(); 


         return $this->redirect(['view', 'id' => $model->id]); 
        } else { 
         return $this->render('update', [ 
          'model' => $model, 
         ]); 
        } 
       } 

       /** 
       * Deletes an existing UserList model. 
       * If deletion is successful, the browser will be redirected to the 'index' page. 
       * @param integer $id 
       * @return mixed 
       */ 
       public function actionDelete($id) 
       { 
        $this->findModel($id)->delete(); 

        return $this->redirect(['index']); 
       } 

       /** 
       * Finds the UserList model based on its primary key value. 
       * If the model is not found, a 404 HTTP exception will be thrown. 
       * @param integer $id 
       * @return UserList the loaded model 
       * @throws NotFoundHttpException if the model cannot be found 
       */ 
       protected function findModel($id) 
       { 
        if (($model = UserList::findOne($id)) !== null) { 
         return $model; 
        } else { 
         throw new NotFoundHttpException('The requested page does not exist.'); 
        } 
       } 
      } 
+0

本の重複:http://stackoverflow.com/questions/13363748これは私のコントローラファイルであります/ yii-password-repeat-fieldしかし、この質問はyii1のためのものです、私はそれを試しています、結果で更新する – ProudNoob

+1

あなたのコントローラコードを表示!パスワードが空の場合はコントローラにチェックを入れ、次にデータベースを更新しないでください。 –

+0

ビューのフォームが空であるかどうかを確認する方法の例を投稿してもよろしいですか? – ProudNoob

答えて

1
if(!empty($_POST['UserList']['password']){ 
--- 
} 

試してみてください:おそらく

public function actionUpdate($id) 
       { 
        $model = $this->findModel($id); 

        if ($model->load(Yii::$app->request->post())) { 
         if(!empty($_POST['UserList']['password']){ 
         $model->password_hash=Yii::$app->getSecurity()->generatePasswordHash($_POST['UserList']['password']); 
        } 
         $model->save(); 


       } 
+0

それは動作します!どうもありがとうございました。 EDIT:ああ、このフィールドを空白のままにすると、「ハッシュが無効です」と表示されました。 – ProudNoob

+0

お手伝いをしてうれしいです。乾杯:) –

+0

もう一度お悔やみして申し訳ありませんが、フィールドを空白のままにしたときに「ハッシュが無効です」と表示されました。それはちょうど一度働いた。編集:ちょっと、 "password_hash"フィールドは空白です。 – ProudNoob

関連する問題