phplaravellaravel-validation

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

Solution

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