Laravel - string matching

When I use the check function in Laravel, how can I add predefined strings that are allowed in the input?

For example, suppose I want Input to contain only one of the following: married,single,gay , how can I do this?

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

In laravel docs, it is recommended that you add validation rules to the array when they get larger, and I thought 3 rules were sufficient. I think this makes it for more readable content, but you don't need it. I added the regex part below, and it works for me. I am not so good at regular expressions. Let me know.

 $validator = Validator::make($credentials, [ 'profile' => ["required" , "max:255", "regex:(married|single|gay)"] ]); 
+5
source

It is best to use the 'in' validation rule as follows:

 $validator = Validator::make($credentials, [ 'profile' => ["required" , "max:255", "in:married,single,gay"] ]); 
+10
source

All Articles