Laravel text area validation best practice

Dmi*_*lys 5 php laravel

I am not sure what kind of validation I should use for comments in E-commerce website that I am currently developing. It is not much I want to validate but I'm worried about security.

So what is the best practice?

My code now looks like this:

$this->validate($request, [
    'comment' => 'max:1000',
]);
Run Code Online (Sandbox Code Playgroud)

Is it safe to leave it like that?

Mah*_*esi 6

这取决于您的需求,但我可以建议一些

$this->validate($request, [
    'comment' => 'required|min:3|max:1000',
]);
Run Code Online (Sandbox Code Playgroud)

  • 为什么你希望你的评论可以为空?那不是说我可以提交空评论吗?我看不出重点!! (2认同)
  • 是的,你是对的,所以如果是评论应该有一个正文,所以添加 required 到规则 (2认同)
  • 必需限制和可为空限制之间的区别在于,当字段设置为必需时,数据永远不会在注释为空的情况下提交。由于您正在开发电子商务,因此最佳实践是只让愿意的客户发表评论,而不是强迫他们提交评论。这就是为什么我鼓励它可以为空。 (2认同)