M
M
Maxim Lagoysky2017-03-22 16:06:09
Yii
Maxim Lagoysky, 2017-03-22 16:06:09

Own validation in the module does not work, what's wrong?

Started to learn yii2 and faced such problem. I created a separate module for the user, made a registration form, wrote a rule for the fields and wrote one rule for email and it doesn’t work for the life of me, although if you copy it and paste it into the standard models folder which is at the very top, everything works there, I tried to use scripts the result is the same. Help me solve the problem, otherwise they are already taking horses from me.

<?php
/**
 * Created by PhpStorm.
 * User: lagoy
 * Date: 22.03.2017
 * Time: 10:22
 */

namespace app\modules\user\models;

use yii\base\Model;

class UserRegForm extends Model
{
    public $email;
    public $password;
    public $password_repeat;

    public function rules()
    {
        return [
            [['email','password', 'password_repeat'],'required'],
            [['email'], 'email'],
            ['password','string','min'=>6,'max'=>20],
            ['password_repeat','string','min'=>6,'max'=>20],
            ['email', 'vlemail', 'on' => 'reg']
        ];
    }

    public function vlemail($attribute,$params)
    {
        if(!$this->hasErrors()) // если нет ошибок в валидации
        {
            if($this->email != '[email protected]'){
                $this->addError($attribute,'Мое правило сработало');
            }
        }
    }

}

Here is the controller
<?php
/**
 * Created by PhpStorm.
 * User: lagoy
 * Date: 22.03.2017
 * Time: 10:32
 */

namespace app\modules\user\controllers;

use app\modules\user\models\user;
use app\modules\user\models\UserRegForm;
use Yii;

class UserController extends UserAppController
{
    public function actionRegister(){
        $model = new UserRegForm();
        $model->scenario = 'reg';
        return $this->render('register', compact('model'));
    }
}

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
Maxim Fedorov, 2017-03-22
@lagoy

uh why should it work? You don't validate the model anywhere... or do you mean that it should work on client-side validation? If so, then you should implement your own validator class and implement a client-side data validation mechanism in it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question