Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating Matching Strings

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

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
]);
like image 408
Neel Avatar asked Jul 15 '15 19:07

Neel


People also ask

How do you validate a string?

To validate a string for alphabets you can either compare each character in the String with the characters in the English alphabet (both cases) or, use regular expressions.

What is string validation?

The StringValidator is used to test that a value conforms to a set of validation constraints. The validator is passed a JavaScript string. If a string is a masked string, it should be converted (using the MaskConverter) to a string (stripped of literals) before it is passed to the validator.


1 Answers

Best to use the 'in' validation rule like this:

$validator = Validator::make($credentials, [
      'profile' => ["required" , "max:255", "in:foo,bar,baz"]  
]);
like image 192
omarjebari Avatar answered Sep 23 '22 15:09

omarjebari