Reputation: 89
I am validating form with formRequest
, I have defined $redirect
. Now I want to handle validation error in $redirect
function.
Upvotes: 5
Views: 1608
Reputation: 89
I am able to create the json with error message from formRequest but without $redirect.
Here is my code,
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
use Response;
class CreateUserRequest extends Request {
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'email' => 'required'
];
}
public function messages(){
return [
'email.required' => 'Er, you forgot your email address!'
];
}
public function response(array $errors)
{
return Response::json($errors, 400);
}
}
Upvotes: 3
Reputation: 8663
I have error message display blade partial that looks like below. It works both for redirected messages when variables are in session and while returning the view() where variables are directly accessible
<div class = "container">
@unless($errors->count()==0)
@foreach($errors->all() as $err)
<p class = "alert alert-danger col-md-6 col-md-offset-3 animated slideInUp">{{$err}}</p>
@endforeach
@endunless
@unless(Session::get('myerror')==null)
<p class = "alert col-md-6 col-md-offset-3 alert-danger animated slideInUp">{{Session::get('myerror')}}</p>
@endunless
@if(isset($myerror))
<p class = "alert col-md-6 col-md-offset-3 alert-danger animated slideInUp">{{$myerror}}</p>
@endif
If you want to return JSON response with error messages then here are some tips of how to set the content and set your own response codes and headers http://laravel.com/docs/5.0/responses
Upvotes: 0