我需要从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'));
}],
];
}最后一条规则是我自己添加的过滤器。但它不起作用。标签、空格、qoutes不会被删除,这个过滤器甚至不会运行。如何实现我想要的,以及我做错了什么?
谢谢
发布于 2015-01-27 18:41:50
您添加的验证器错误。如果你想使用FilterValidator (你在问题中提到的)而不是内联验证器,像这样修改你的代码:
[['name', 'email', 'phone'], 'filter', 'filter' => function($value) {
return trim(htmlentities(strip_tags($value), ENT_QUOTES, 'UTF-8'));
}],['name', 'email', 'phone']验证的属性。
filter -验证器短名称。请参阅合规性here的完整列表。
接下来的元素是将被传递给这个验证器的参数。在本例中,我们指定了filter参数。
在官方文档中查看特定验证器的完整可用参数列表。
https://stackoverflow.com/questions/28168253
复制相似问题