phplaravellaravel-validation

Laravel - Validate value against string


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?


Solution

  • 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')]),
        ]    
    ]);