How to validate exact words in Laravel?

后端 未结 2 669
情书的邮戳
情书的邮戳 2021-02-12 18:45

Does the validation class in Laravel validate exact words?

eg

$rules = [
           \"field\" => \"hello\"
         ]

Where the val

相关标签:
2条回答
  • 2021-02-12 19:25

    Yes. I know of at least two ways.

    // option one: 'in' takes a comma-separated list of acceptable values
    $rules = [
        'field' => 'in:hello',
    ];
    
    // option two: write a matching regular expression
    $rules = [
        'field' => 'regex:^hello$',
    ];
    

    Actually, I don't remember if you need the ^$ delimiters in the regex, but it's easy to find out by trying it. :)

    0 讨论(0)
  • 2021-02-12 19:37

    Method updated for Laravel 5.5. https://laravel.com/docs/5.5/validation#rule-in

    // 'in' takes an array of values
    
    $rules = [
        'field' => [ Rule::in('hello')]
    ];
    

    Use Rule; namespace in your controller to access Rule class (Illuminate\Validation\Rule)

    0 讨论(0)
提交回复
热议问题