Hi! It’s famous problem.
I found many information in other sites, blogs and forums, but i don’t can fix this problem in my situation… =( Please help someone!
I use basic version. And i create functionality for registration new user (in base version of this is not).
My form in view:
<?php $form = ActiveForm::begin([
'id' => 'registration-form',
'options' => ['class' => 'form-horizontal'],
'enableAjaxValidation' => true,
'fieldConfig' => [
'template' => "{label}\n<div class=\"col-lg-4\">{input}</div>\n<div class=\"col-lg-6\">{error}</div>",
'labelOptions' => ['class' => 'col-lg-2 control-label'],
],
]); ?>
<?= $form->field($model, 'name')->textInput(['autofocus' => true]) ?>
<?= $form->field($model, 'email')->textInput() ?>
<?= $form->field($model, 'password')->passwordInput() ?>
<?= $form->field($model, 'verifypassword')->passwordInput() ?>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<?= Html::submitButton('Registration', ['class' => 'btn btn-primary', 'name' => 'reg-button']) ?>
</div>
</div>
<?php ActiveForm::end(); ?>
My action:
public function actionRegistration()
{
if (Yii::$app->request->post())
{
$modeluser = Yii::$app->request->post()['RegistrationForm'];
$newUser = new User();
$newUser->loadDefaultValues();
$newUser->name = $modeluser['name'];
$newUser->email = $modeluser['email'];
$newUser->password = md5($modeluser['password']);
$newUser->save();
if (Yii::$app->user->login(UserIdentity::findByEmail($modeluser['email'])))
{
return $this->goHome();
}
return $this->goBack();
}
$model = new RegistrationForm();
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post()))
{
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
return \yii\widgets\ActiveForm::validate($model);
}
return $this->render('registration', ['model' => $model]);
}
And my model:
public function rules()
{
return [
[['name', 'email', 'password', 'verifypassword'], 'required'],
['name', 'filter', 'filter' => 'trim'],
['password', 'filter', 'filter' => 'trim'],
['email', 'email'],
['email', 'unique'],
['email', 'validateEmail'],
['password', 'string', 'length'=>[6]],
['verifypassword', 'compare', 'compareAttribute'=>'password', 'operator'=>'==='],
];
}
public function validateEmail($attribute, $params)
{
$this->addError($attribute, 'Email most be unique!');
}
In function validateEmail i deliberately add error to my form. And? When i fill ONLY!!! e-mail field and this data already exist in DB and i remove focus from this field, happen login user. Other field does not matter, because for login i need email and password. If disable option ‘enableAjaxValidation’ => true in form then don’t submit but and i don’t have checking by my condition!
WTF???