• Можно ли получить ошибки валидации формы без виджетов yii2?

    @ivan_dv
    Model
    <?php 
    
    namespace app\models\user\forms;
    
    
    use Yii;
    use yii\base\Model;
    use app\models\user\User;
    
    
    class RegisterForm extends Model 
    {
        public $firstname;
        public $lastname;
        public $email;
        public $email_repeat;
        public $password;
        public $password_repeat;
    
        public function rules()
        {
            return [
                [['firstname', 'lastname', 'email', 'email_repeat', 'password', 'password_repeat'], 'required'],
        
                [['firstname', 'lastname','email', 'email_repeat',  'password', 'password_repeat'], 'trim'],
        
                [['firstname', 'lastname'], 'string', 'min'=>2, 'max'=>20],
        
                [['password'], 'string', 'min'=>6, 'max' => 12],
        
                [['password_repeat'], 'compare', 'compareAttribute'=>'password', 'message'=>"Password dont match"],
    
                [['email_repeat'], 'compare', 'compareAttribute'=>'email', 'message'=>"Email dont match"],
        
                [['email'], 'email'],
    
                [['email'], 'checkUniqueEmail']
              ];
        }
    
        public function register()
        {
            // $firstname, $lastname, $email, $password
            $user = new User();
    
            $user->firstname = $this->firstname;
    
            $user->lastname = $this->lastname;
    
            $user->email = $this->email;
    
            $user->password = $user->setPassword($this->password);
    
            $user->auth_key = $user->generateToken();
    
            $user->email_token = $user->generateToken();
    
            $user->status = User::USER_INACTIVE;
    
            $user->created_at = time();
    
            $user->updated_at = time();
    
            return $user->save();
        }
    
        public function checkUniqueEmail($attribute, $params)
        {
                $email = User::find()->where(['email'=> $this->email])->one();
                if ($email)
                 {
                    $this->addError($attribute, 'email exists');
                    return false;
                }
            
        }
    }


    Controller

    public function actionTest()
        {
            $model = new RegisterForm();
            
            if($model->load(Yii::$app->request->post()))
            {
               if ($model->validate()) 
               {
                $model->register();
               }
            }
            return $this->render('test',[
                'model' => $model,
            ]);
        }


    View

    use yii\helpers\Html;
    
    <?= Html::beginForm(['site/test'], 'post', ['enctype' => 'multipart/form-data', 'class' => 'reg-fr']) ?>
    
    <?= Html::input('text', 'RegisterForm[firstname]', null, ['class' => 'tes']) ?>
    <?= Html::error($model, 'firstname') ?>
    
    <?= Html::input('text', 'RegisterForm[lastname]', null, ['class' => 'hello']) ?>
    <?= Html::error($model, 'lastname') ?>
    
    <?= Html::input('text', 'RegisterForm[email]', null, ['class' => 'hello']) ?>
    <?= Html::error($model, 'email') ?>
    
    <?= Html::input('text', 'RegisterForm[email_repeat]', null, ['class' => 'hello']) ?>
    <?= Html::error($model, 'email_repeat') ?>
    
    <?= Html::input('text', 'RegisterForm[password]', null, ['class' => 'hello']) ?>
    <?= Html::error($model, 'password') ?>
    
    <?= Html::input('text', 'RegisterForm[password_repeat]', null, ['class' => 'hello']) ?>
    <?= Html::error($model, 'password_repeat') ?>
    
    <input type="submit" value="send">
    
    <?= Html::endForm() ?>
    Ответ написан
    Комментировать