Custom validation filter on yii2

I need to filter the data that comes from the hmtl form from html tags, quotes, etc.

It seems I need to write my own filter callback function according to http://www.yiiframework.com/doc-2.0/yii-validators-filtervalidator.html . I got these rules in my model:

 public function rules()
    {
        return [
            [['name', 'email', 'phone',], 'required'],
            [['course'], 'string',],
            [['name', 'email', 'phone',], 'string', 'max'=>250],
            ['email', 'email'],
            [['name', 'email', 'phone'], function($value){
                return trim(htmlentities(strip_tags($value), ENT_QUOTES, 'UTF-8'));
            }],

        ];
    }

The last rule is my own filter, which I added. But that does not work. Tags, spaces, qoutes are not removed, and this filter does not even work. How to achieve what I want and what I am doing wrong?

thank

+4
source share
1 answer

. FilterValidator ( ), , :

[['name', 'email', 'phone'], 'filter', 'filter' => function($value) {
    return trim(htmlentities(strip_tags($value), ENT_QUOTES, 'UTF-8'));
}],

['name', 'email', 'phone'] - .

filter - . .

- , . filter.

. .

+4

All Articles