Step 1 : php artisan make:request StoreArticleRequest
Step 2 :
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\JsonResponse;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;
class StoreArticleRequest extends FormRequest
{
/**
* 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 [
'title' => 'required'
];
}
protected function failedValidation(Validator $validator)
{
if ($this->wantsJson() || $this->ajax()) {
throw new HttpResponseException(
response()->json([
'status' => 'error',
'message' => 'Please enter all the required fields',
'errors' => $validator->errors()->all(),
'result' => null
], 200)
);
}
parent::failedValidation($validator);
}
}
Comments
Post a Comment