我想清理我的应用程序的用户输入以下this articleLaravel 5消毒必填字段行为
下面是我的要求
class TestRequest extends Request
{
public function authorize()
{
return true;
}
public function rules()
{
$this->sanitize();
return [
'title'=>'required|max:100'
];
}
public function sanitize()
{
$input = $this->all();
if(!empty($input))
{
$input['title'] = trim(strip_tags($input['title']));
$this->replace($input);
}
}
}
艰难的标题是必填字段,如果我试图把<h1></h1>
作为标题字段中的输入,根据sanitize()
函数中的逻辑,标签被剥离,但空字符串被保存在数据库中。规则中所需的字段验证不起任何作用。
如何处理?
更新:
下面是用于保存请求的控制器方法。
public function save(TestRequest $request)
{
$input = $request->all();
...
}
请显示相关的控制器方法。 –
@AlexeyMezenin控制器方法添加.. – ZedBee