Created
January 7, 2024 12:22
-
-
Save LucasHayashi/eb6aae535f4f7896188114d8da475cd2 to your computer and use it in GitHub Desktop.
Sobrescrita do método failedValidation para retornar os erros em JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Requests; | |
use Illuminate\Foundation\Http\FormRequest; | |
use Illuminate\Contracts\Validation\Validator; | |
use Illuminate\Http\Exceptions\HttpResponseException; | |
class StoreCategoryRequest extends FormRequest | |
{ | |
/** | |
* Determine if the user is authorized to make this request. | |
*/ | |
public function authorize(): bool | |
{ | |
return true; | |
} | |
/** | |
* Get the validation rules that apply to the request. | |
* | |
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string> | |
*/ | |
public function rules(): array | |
{ | |
return [ | |
'name' => ['required'], | |
'description' => ['max:255'] | |
]; | |
} | |
public function messages() | |
{ | |
return [ | |
'name.required' => 'O campo nome é obrigatório.', | |
'description.max' => 'A descrição não pode ter mais de 255 caracteres.', | |
]; | |
} | |
protected function failedValidation(Validator $validator) | |
{ | |
throw new HttpResponseException(response()->json($validator->errors(), 422)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment