[[+content_image]]
L
L
lilwings2020-02-09 19:53:26
Yii
lilwings, 2020-02-09 19:53:26

How to properly write such a check in Yii2?

public function rules() {
    return [
        [['title', 'preview'], 'string', 'max' => 255, 'filter', 'filter' => ['strip_tags', 'trim'], 'required']
    ];
}

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
S
Sergey Kupletsky, 2020-02-09
@lilwings

Each validator is specified separately (I did not see the built-in strip_tags validator in the documentation , so I replaced it with a function):

return [
  [['title', 'preview'], 'string', 'max' => 255],
  [['title', 'preview'], 'trim'],
  [['title', 'preview'], 'filter', 'filter' => 
    function ($value) {
      return strip_tags($value);
    }
  ],
  [['title', 'preview'], 'required'],
];

Look at the validation documentation, there are many examples:
https://www.yiiframework.com/doc/guide/2.0/en/tuto...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question