Правила модели:
public function rules()
{
return [
[['username', 'password_hash', 'status'], 'required'],
['username', 'unique'],
['password_hash', 'string', 'min' => 6, 'max' => 255],
[['auth_key'], 'string', 'max' => 32],
['status', 'in','range' => [
User::STATUS_ACTIVE,
User::STATUS_NOT_ACTIVE,
]]
];
}
}
действие контроллера
public function actionReg()
{
$model = new RegForm();
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = 'json';
return \yii\widgets\ActiveForm::validate($model);
}
if($model->load(Yii::$app->request->post()) && $model->validate()){
if($user = $model->reg()){
if($user->status === User::STATUS_ACTIVE){
if(Yii::$app->getUser()->login($user)){
return $this->goHome();
}
}
}else{
echo '<pre>';
Yii::$app->session->setFlash('error','Возникла ошибка при регистрации');
Yii::error('Возникла ошибка при регистрации');
return $this->refresh();
}
}
return $this->render('reg',
['model' => $model]
);
}
Вид:
<?php $form = ActiveForm::begin(['id' => 'registration-form',])?>
<?= $form->field($model, 'username',['enableAjaxValidation' => true]) ?>
<?= $form->field($model, 'password_hash')->passwordInput() ?>
<?= $form->field($model, 'password_repeat')->passwordInput() ?>
<?= $form->field($model, 'status')->hiddenInput()?>
<div class="form-group">
<div>
<?= Html::submitButton('Зарегистрироваться', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?>
</div>
</div>
<?php ActiveForm::end(); ?>