N
N
nepster-web2014-04-13 19:24:20
Yii
nepster-web, 2014-04-13 19:24:20

Yii2 rules (data validation), why do validation errors occur?

So there is a static page module containing the following fields in the pages table:
['title', 'description', 'keywords', 'name', 'url', 'text', 'status']
My validation rules:

public function rules()
  {
    return [
        // Обязательные поля
        [['name', 'text', 'url', 'status'], 'required'],

      // Заголовок, описание, ключевые слова и название страницы
      ['title,description,keywords,name', 'filter', 'filter' => 'trim'],
      ['title,description,keywords,name', 'string', 'max' => 100],

            // статус 
            ['status', 'in', 'range' => array_keys(self::getStatus())],

      // Текст [[text]]
            
            // Url адрес
      ['url', 'unique', 'attributes'=>'url'],
      ['url', 'string', 'max'=>16],
      ['url', 'match', 'pattern'=>'/[a-zA-Z0-9-_.]+$/'],
    ];
  }

There are 2 problems here:
1) in the url field, I can add a space. That is, the regular expression /[a-zA-Z0-9-_.]+$/ somehow works, but it skips the space.
2) some problem with the text field , the situation is such that if I fill in the data and click the save button, the error "Fill in the required text field" pops up and the data is saved from the second click.
Can you please tell me how to solve these problems?

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
Y
Yuri Morozov, 2014-04-13
@nepster-web

1. Write a metacharacter for the beginning of the string, otherwise the regular expression will work on characters at the end.
And escape the dot :)
2. enable validateOnType

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question