Laravel 的驗證類別可以驗證精確值嗎?

DDD
發布: 2024-11-14 17:41:02
原創
923 人瀏覽過

Can Laravel's Validation Class Validate Exact Values?

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中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板