Answer the question
In order to leave comments, you need to log in
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-_.]+$/'],
];
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question