yii2上的自定义验证筛选器
我需要从html标记、引号等中过滤来自hmtl表单的数据 似乎我需要根据以下内容编写自己的过滤器回调函数。我在我的模型中得到了这些规则:yii2上的自定义验证筛选器,yii2,Yii2,我需要从html标记、引号等中过滤来自hmtl表单的数据 似乎我需要根据以下内容编写自己的过滤器回调函数。我在我的模型中得到了这些规则: public function rules() { return [ [['name', 'email', 'phone',], 'required'], [['course'], 'string',], [['name', 'email', 'phone',],
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'));
}],
];
}
最后一条规则是我自己添加的过滤器。但它不起作用。标记、空格和QOUTE不会从中删除,并且此筛选器甚至没有运行。如何实现我想要的和我做错了什么
谢谢您添加的验证程序错误。如果您想使用
FilterValidator
(您在问题中提到过)而不是内联验证器,请按如下方式更改代码:
[['name', 'email', 'phone'], 'filter', 'filter' => function($value) {
return trim(htmlentities(strip_tags($value), ENT_QUOTES, 'UTF-8'));
}],
['name','email','phone']
-已验证的属性
过滤器
-验证程序短名称。请参阅完整的合规性列表
接下来的元素是将传递到此验证器的参数。在本例中,我们指定了参数
请参阅官方文档中针对特定验证器的可用参数的完整列表