验证匹配字符串 [英] Validating Matching Strings

查看:59
本文介绍了验证匹配字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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)"]  
]);

这篇关于验证匹配字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆