How to validate an input matches one or more exact values in Laravel?
Asked Answered
C

2

26

Does the validation class in Laravel validate exact words?

eg

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

Where the value of the field must be "hello".

Chevalier answered 7/5, 2014 at 5:56 Comment(0)
J
56

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. :)

Jackdaw answered 7/5, 2014 at 6:1 Comment(0)
C
7

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)

Chadwell answered 19/12, 2017 at 5:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.