1

Validation code

 $this->validate($request, [
            'email'=> 'required|email|unique:users',
            'email'=> 'required|max:120',
            'password' => 'required|min:4'
            ]);

how can i return the errors of validation if they exist as a json response?

  • This should work, what are you trying to do? How do you make the request, in a browser or via postman? – Björn Jul 06 '17 at 22:35

1 Answers1

1

Using $this->validate on the controller action automates the whole process significantly. If the validation fails, it automatically redirects to the previous page, while a list of errors should be available in $errors variable in your view.

If you want to have a control over the process you can do it that way:

$validator = Validator::make($request->all(), [
        'title' => 'required|unique:posts|max:255',
        'body' => 'required',
]);

if ($validator->fails()) {
  return $validator->errors();
}

// Validation successful

Please see https://laravel.com/docs/5.4/validation for more information.

Chris Cynarski
  • 493
  • 3
  • 9