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.
以上是Laravel 的驗證類別可以驗證精確值嗎?的詳細內容。更多資訊請關注PHP中文網其他相關文章!