パスワード変更機能を使用しようとしているときに、ユーザーが入力するフォームを作成して、ハッシュして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.');
}
}
}
本の重複:http://stackoverflow.com/questions/13363748これは私のコントローラファイルであります/ yii-password-repeat-fieldしかし、この質問はyii1のためのものです、私はそれを試しています、結果で更新する – ProudNoob
あなたのコントローラコードを表示!パスワードが空の場合はコントローラにチェックを入れ、次にデータベースを更新しないでください。 –
ビューのフォームが空であるかどうかを確認する方法の例を投稿してもよろしいですか? – ProudNoob