Ciao a tutti,
è tutto il giorno che sto impazzendo sulla validazione della password.
In pratica ho due campi per inserire la password: password_hash e password_hash_repeat
Il form funziona ma anzichè salvare mi da errore BAD REQUEST #400 Missing required parameters: id
Vi posto il codice :
_form.php
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model backend\models\lista_clienti */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="lista-clienti-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'username')->textInput(['maxlength' => 30]) ?>
<?= $form->field($model, 'firstname')->textInput(['maxlength' => 120]) ?>
<?= $form->field($model, 'lastname')->textInput(['maxlength' => 120]) ?>
<!--<?= $form->field($model, 'auth_key')->textInput(['maxlength' => 32]) ?>-->
<?= $form->field($model, 'password_hash')->textInput(['maxlength' => 30]) ?>
<?= $form->field($model, 'password_hash_repeat' )->textInput(['maxlength' => 30]) ?>
<?= $form->field($model, 'email')->textInput(['maxlength' => 255]) ?>
<!-- <?= $form->field($model, 'status')->textInput() ?>
<?= $form->field($model, 'created_at')->textInput() ?>
<?= $form->field($model, 'updated_at')->textInput() ?>-->
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
MODEL - lista-clienti
<?php
namespace backend\models;
use Yii;
//aggiunte
use common\models\User;
use yii\base\Model;
/**
* This is the model class for table "user".
*
* @property integer $id
* @property string $username
* @property string $firstname
* @property string $lastname
* @property string $auth_key
* @property string $password_hash
* @property string $password_reset_token
* @property string $email
* @property integer $status
* @property integer $created_at
* @property integer $updated_at
*/
class lista_clienti extends \yii\db\ActiveRecord
{
public $password_hash_repeat;
/**
* @inheritdoc
*/
public static function tableName()
{
return 'user';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
['username', 'filter', 'filter' => 'trim'],
['username', 'required', 'message'=> 'Inserire un username'],
['username', 'unique', 'targetClass' => '\common\models\User', 'message' => 'Username già usato'],
['username', 'string', 'min' => 5, 'max' => 20, 'tooShort'=> 'Minimo 5 caratteri', 'tooLong' => 'Massimo 20 caratteri alfanumerici'],
[['firstname', 'lastname'], 'required', 'message' => ' Campo obbligatorio'],
[['firstname', 'lastname'], 'string', 'min'=>3, 'max' => 120, 'tooShort'=> ' Minimo 3 caratteri'],
//['password_hash_repeat', 'message' => 'Inserire una password'],
['password_hash', 'required'],
['password_hash', 'string', 'min' => 6, 'tooShort' => 'Minimo 6 caratteri alfanumerici'],
//['password_hash_repeat', 'required', 'message' => ' Ripetere la password'],
['password_hash_repeat', 'required', 'message' => ' Ripetere la password'],
[['password_hash_repeat'], 'compare', 'compareAttribute' => 'password_hash'],
//['password_hash', 'compare'],//password_repeat
//['password_hash_repeat','safe'],
['email', 'filter', 'filter' => 'trim'],
['email', 'required', 'message' => ' Inserire una email valida'],
['email', 'email', 'message' =>'Email non valida'],
['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'Email già usata'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'username' => 'Username',
'firstname' => 'Nome',
'lastname' => 'Cognome',
'password_hash' => 'Password',
'password_hash_repeat' => 'Ripeti Password ',
'email' => 'Email',
];
}
}
CONTROLLER - ListaClientiControllers
<?php
namespace backend\controllers;
use Yii;
use backend\models\lista_clienti;
use backend\models\ListaClientiSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
//aggiunte
use yii\base\InvalidParamException;
use yii\web\BadRequestHttpException;
use yii\filters\AccessControl;
/**
* ListaClientiController implements the CRUD actions for lista_clienti model.
*/
class ListaClientiController extends Controller
{
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}
/**
* Lists all lista_clienti models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new ListaClientiSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single lista_clienti model.
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new lista_clienti model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new lista_clienti();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->status = 10;
$model->created_at = date('d-m-Y');
$hash = Yii::$app->getSecurity()->generatePasswordHash($model->password_hash);
$model->password_hash = $hash;
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
/**
* Updates an existing lista_clienti 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 = md5($this->password_hash);
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
/**
* Deletes an existing lista_clienti 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 lista_clienti model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return lista_clienti the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = lista_clienti::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
Dove sbaglio
Grazie a tutti in anticipo!!!