验证匹配字符串

编程入门 行业动态 更新时间:2024-10-28 18:33:44
本文介绍了验证匹配字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在Laravel中使用验证功能时,如何添加输入中允许的预定义字符串?

When I use the Validation feature in Laravel, how can I add a pre-defined strings that are allowed in an Input?

例如,假设我希望输入仅包含以下之一:foo,bar,baz,我该怎么做?

For example, let's say I want the Input to contain only one of the following: foo,bar,baz, how can I do that?

$validator = Validator::make($credentials, [ 'profile' => 'required|max:255', // Here I want Predefined allowed values for it ]);

推荐答案

在Laravel文档中,建议当验证规则变得更大并且我认为3条规则就足够时,将验证规则放入数组中.我认为它使内容更具可读性,但您不必这样做.我在下面添加了regex部分,并且它可以正常工作.我对重排东西不是很好.让我知道.

It is recommended in Laravel docs to put the validation rules in an array when they get bigger and I thought 3 rules were sufficient. I think it makes it for a more readable content but you do not have to. I added the regex portion below and I have it works. I am not that great with regexing stuff. Let me know.

$validator = Validator::make($credentials, [ 'profile' => ["required" , "max:255", "regex:(foo|bar|baz)"] ]);

更多推荐

验证匹配字符串

本文发布于:2023-11-12 07:57:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580940.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!