Validate Exact Matching Input Values in Laravel
When validating input in Laravel, it is often necessary to ensure that a certain field matches an exact value. The validation class provides multiple options for performing this type of validation.
Option 1: Using 'in'
The 'in' rule can be used to validate that the input value is equal to one of a comma-separated list of acceptable values. For example, to require that the "field" field can only have the value "hello," the following rule can be used:
$rules = [ 'field' => 'in:hello' ];
Option 2: Using Regular Expression
A regular expression can also be used to match the input value against a specific pattern. To require that the "field" field contain exactly the word "hello," the following rule can be used:
$rules = [ 'field' => 'regex:^hello$' ];
Additional Note
While the examples provided use the '^' and '$' delimiters in the regular expression, these delimiters may not be necessary depending on the specific regular expression used.
The above is the detailed content of How to Validate Exact Matching Input Values in Laravel?. For more information, please follow other related articles on the PHP Chinese website!