Validating Exact Values with Laravel's Validation Class
Does Laravel's validation class allow for validating an input to match one or more exact values?
For example, consider the following validation rule:
$rules = [ "field" => "hello" ];
In this case, we want to ensure that the value of the "field" input is exactly "hello".
Answer:
Yes, Laravel's validation class provides two options for validating exact values:
Option 1: Using the 'in' Rule
The 'in' rule takes a comma-separated list of acceptable values. For our example, the validation rule would be:
$rules = [ 'field' => 'in:hello', ];
Option 2: Using a Regular Expression
A regular expression can also be used to match exact values. In our example, the regex would be:
$rules = [ 'field' => 'regex:^hello$', ];
The "^" and "$" delimiters ensure that the entire input value matches the regex.
Note that you may not need the "^$" delimiters in the regex, but it is worth testing to confirm.
The above is the detailed content of Can Laravel's Validation Class Validate Exact Values?. For more information, please follow other related articles on the PHP Chinese website!