2015-04-24 8 views

Odpowiedz

0

Mam komunikat o błędzie, który wygląda jak poniżej. Działa zarówno dla przekierowanych komunikatów gdy zmienne są w sesji i podczas powrotu pogląd() gdzie zmienne są bezpośrednio dostępne

<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 

Jeśli chcesz powrócić odpowiedź JSON z komunikatów o błędach, to oto kilka wskazówek z jak ustawić zawartość i ustawić własne kody odpowiedzi i nagłówki http://laravel.com/docs/5.0/responses

+0

Jestem nowy laravel-5 i działa na REST API, od listonosz jestem w stanie sprawdzić ładunek JSON z formularza prośby. Ale mam do czynienia z problemem w odpowiedzi json. Jak zwracać komunikaty o błędach? – user2893940

+0

Edytowałem odpowiedź, aby odzwierciedlić Twój komentarz. Jeśli masz jakiś konkretny błąd, daj nam znać. –

3

Jestem w stanie utworzyć json z komunikatem o błędzie z formRequest, ale bez $ redirect.

Oto mój kod,

<?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); 
    } 
} 
Powiązane problemy