1
1
19prv942015-05-11 18:24:56
Yii
19prv94, 2015-05-11 18:24:56

Why is the validator not being called in Yii2?

Started learning Yii2, met a problem with form validation.

public function requiredTitile($attribute, $options) {
    if (!$this->title) {
      $this->addError('title', 'Нужно ввести название');
    } else if (count($this->title) < 5) {
      $this->addError('title', 'Минимальная длина - 5 символов');
    }
  }
  
  public function rules()
  {
    return array([['title'], 'requiredTitile']);
  }

When you enter any value in the title field, the validator works, but when the field is empty, it does not. Where did I go wrong?

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
matperez, 2015-05-11
@19prv94

Most likely this is www.yiiframework.com/doc-2.0/yii-validators-valida...

S
Sergey Semenko, 2015-05-11
@abler98

For empty values, there is a required validator, otherwise it is not required and it fails validation.

M
Maxim Grechushnikov, 2015-05-11
@maxyc_webber

in our case there is already a validator
string length=>[0,50]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question