2015-05-29 12 views
6

Ho una richiesta di modulo per convalidare i dati di registrazione. L'applicazione è un'API mobile e vorrei che questa classe restituisse il JSON formattato in caso di convalida fallita invece di quello che fa per impostazione predefinita (reindirizzamento).Laravel 5 richiesta di convalida fallita della richiesta di modifica modulo

Ho provato a ignorare il metodo failedValidation dalla classe Illuminate\Foundation\Http\FormRequest. ma non sembra funzionare. Qualche idea?

Codice:

<?php 

namespace App\Http\Requests; 

use App\Http\Requests\Request; 
use Illuminate\Contracts\Validation\Validator; 

class RegisterFormRequest 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' => 'email|required|unique:users', 
     'password' => 'required|min:6', 
    ]; 
} 

} 
+2

si prega di inviare il vostro codice per le persone a controllare. – SuperBiasedMan

+0

Immagino che tu stia chiamando la tua API via AJAX? Puoi forzare la chiamata all'API per aspettarti JSON dalla tua API? In jQuery sembra che: $ .getJSON. – ChainList

risposta

1

Osservando seguente funzione nel Illuminate\Foundation\Http\FormRequest, sembra laravel gestisce in modo corretto.

/** 
    * Get the proper failed validation response for the request. 
    * 
    * @param array $errors 
    * @return \Symfony\Component\HttpFoundation\Response 
    */ 
    public function response(array $errors) 
    { 
     if ($this->ajax() || $this->wantsJson()) 
     { 
      return new JsonResponse($errors, 422); 
     } 

     return $this->redirector->to($this->getRedirectUrl()) 
             ->withInput($this->except($this->dontFlash)) 
             ->withErrors($errors, $this->errorBag); 
    } 

E come per wantsJson funzione Illuminate\Http\Request di seguito, si deve cercare in modo esplicito JSON risposta,

/** 
    * Determine if the current request is asking for JSON in return. 
    * 
    * @return bool 
    */ 
    public function wantsJson() 
    { 
     $acceptable = $this->getAcceptableContentTypes(); 

     return isset($acceptable[0]) && $acceptable[0] == 'application/json'; 
    } 
0

Questa è la mia soluzione e si sta lavorando bene nella mia fine. Ho aggiunto la funzione seguente Codice richiesta:

public function response(array $errors) 
{ 
    if ($this->ajax() || $this->wantsJson()) 
    { 
     return Response::json($errors); 
    } 

    return $this->redirector->to($this->getRedirectUrl()) 
            ->withInput($this->except($this->dontFlash)) 
            ->withErrors($errors, $this->errorBag); 
} 

La funzione di risposta gestita bene da laravel. Verrà automaticamente restituito se si richiede json o ajax.

4

Non è necessario sovrascrivere alcuna funzione. aggiungete semplicemente il

Accept: application/json 

Nell'intestazione del modulo. Laravel restituirà la risposta nello stesso URL e nel formato JSON.

0

Basta aggiungere la seguente funzione sulla vostra richiesta:

use Response; 
public function response(array $errors) 
{ 
     return Response::json($errors);  
} 
Problemi correlati