@nnkrasovok

Ошибка валидации?

При попытке регистрации выводит ошибку: Invalid validation rule: a rule must specify both attribute names and validator type.

Не могу разобраться.

модель User
public function scenarios()
    {
        return [
            'register' => ['username', 'email', 'password', 'role_id', 'name', 'surname'],
            'connect'  => ['username', 'email'],
            'create'   => ['username', 'email', 'password', 'role', 'role_id', 'name', 'surname'],
            'update'   => ['username', 'email', 'password', 'role', 'role_id', 'name', 'surname'],
            'settings' => ['username', 'email', 'password', 'name', 'surname']
        ];
    }

    /** @inheritdoc */
    public function rules()
    {
        return [
            // username rules
            ['username', 'required', 'on' => ['register', 'connect', 'create', 'update']],
            ['username', 'match', 'pattern' => '/^[a-zA-Z]\w+$/'],
            ['username', 'string', 'min' => 3, 'max' => 25],
            ['username', 'unique'],
            ['username', 'trim'],

            // name rules
            ['name', 'required', 'on' => ['register', 'create', 'update']],
            ['name', 'string', 'min' => 1, 'max' => 24],
            ['name', 'trim'],

            // surname rules
            ['surname', 'on' => ['register', 'create', 'update']],
            ['surname', 'string', 'min' => 1, 'max' => 30],
            ['surname', 'trim'],

            // role_id rules
            ['role_id', 'required', 'on' => ['register', 'create', 'update']],

            // email rules
            ['email', 'required', 'on' => ['register', 'connect', 'create', 'update', 'update_email']],
            ['email', 'email'],
            ['email', 'string', 'max' => 255],
            ['email', 'unique'],
            ['email', 'trim'],

            // unconfirmed email rules
            ['unconfirmed_email', 'required', 'on' => 'update_email'],
            ['unconfirmed_email', 'unique', 'targetAttribute' => 'email', 'on' => 'update_email'],
            ['unconfirmed_email', 'email', 'on' => 'update_email'],

            // password rules
            ['password', 'required', 'on' => ['register', 'update_password']],
            ['password', 'string', 'min' => 6, 'on' => ['register', 'update_password', 'create']],

            // current password rules
            ['current_password', 'required', 'on' => ['update_email', 'update_password']],
            ['current_password', function ($attr) {
                if (!empty($this->$attr) && !Password::validate($this->$attr, $this->password_hash)) {
                    $this->addError($attr, \Yii::t('user', 'Current password is not valid'));
                }
            }, 'on' => ['update_email', 'update_password']],
        ];
    }


модель формы регистрации

public function rules()
    {
        return [
            ['username', 'filter', 'filter' => 'trim'],
            ['username', 'match', 'pattern' => '/^[a-zA-Z]\w+$/'],
            ['username', 'required'],
            ['username', 'unique', 'targetClass' => $this->module->manager->userClass,
                'message' => \Yii::t('user', 'This username has already been taken')],
            ['username', 'string', 'min' => 3, 'max' => 20],

            // name rules
            ['name', 'filter', 'filter' => 'trim'],
            ['name', 'string', 'min' => 1, 'max' => 24],
            ['name', 'required'],

            // surname rules
            ['surname', 'filter', 'filter' => 'trim'],
            ['surname', 'string', 'min' => 1, 'max' => 30],

            // role_id rules
            ['role_id', 'required'],

            ['email', 'filter', 'filter' => 'trim'],
            ['email', 'required'],
            ['email', 'email'],
            ['email', 'unique', 'targetClass' => $this->module->manager->userClass,
                'message' => \Yii::t('user', 'This email address has already been taken')],

            ['password', 'required', 'skipOnEmpty' => $this->module->enableGeneratingPassword],
            ['password', 'string', 'min' => 6],
        ];
    }
  • Вопрос задан
  • 871 просмотр
Решения вопроса 1
@pocifis
PHP-разработчик
['surname', 'on' => ['register', 'create', 'update']]
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы