我需要从html标签,引号等中过滤来自hmtl形式的数据。
看来我需要根据http://www.yiiframework.com/doc-2.0/yii-validators-filtervalidator.html编写自己的过滤器回调函数。我在模型中得到了以下规则:
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'));
}],
];
}
Run Code Online (Sandbox Code Playgroud)
最后一条规则是我添加的我自己的过滤器。但这是行不通的。标签,空格,qoutes不会从中删除,并且此过滤器甚至没有运行。如何实现我想要的和我做错的事情?
谢谢
您添加验证器错误。如果要使用FilterValidator
(您在问题中提到的)而不是内联验证器,请按以下方式更改代码:
[['name', 'email', 'phone'], 'filter', 'filter' => function($value) {
return trim(htmlentities(strip_tags($value), ENT_QUOTES, 'UTF-8'));
}],
Run Code Online (Sandbox Code Playgroud)
['name', 'email', 'phone']
-验证的属性。
filter
-验证者的简称。在此处查看符合性的完整列表。
接下来的元素是将传递到此验证器的参数。在这种情况下,我们指定了过滤器参数。
请参阅官方文档中特定验证器的可用参数的完整列表。