0

I am running some basic validation inside a Laravel 5.5 controller like this...

                   $this->validate($request, [
                        'name' => 'required|max:30',
                        'email' => 'required|unique:users|email',
                        'password' => 'required|max:20',
                        'mykey' => 'required',
                    ]);

Is there a way to check if 'mykey' matches a php string I have saved? I know I can do an if statement and compare them but wondered if there was a way I could do this inside the validation itself?

3
  • 1
    You mean like this? stackoverflow.com/questions/23509802/… Commented Jan 26, 2018 at 15:58
  • I had looked at 'same' and 'confirm' but hadn't spotted 'in' - Will take a look now, thanks Commented Jan 26, 2018 at 16:03
  • Do you mean saved in the database? What should happen if the string is found? Commented Jan 26, 2018 at 16:14

3 Answers 3

2

You can use in rule, This works for n number of values

$request->validate([
    'name' => 'required|max:30',
    'email' => 'required|unique:users|email',
    'password' => 'required|max:20',
    'mykey' => [
        Rule::in([env('MY_KEY'),config('app.another_key')]),
    ]    
]);
Sign up to request clarification or add additional context in comments.

Comments

1

Laravel provides a regex option for validation. Depending on the complexity of the string comparison it may be useful:

https://laravel.com/docs/5.5/validation#rule-regex

Comments

0

You can this rule:

$key = "my_saved_key"
$request->validate([
    'name' => 'required|max:30',
    'email' => 'required|unique:users|email',
    'password' => 'required|max:20',
    'mykey' => 'in:' . $key,
    ]    
]);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.