I'm making a restfull api in Laravel 5.8 and using Postman to send my request. I would like to validate a request like this from postman:
{
"details": [
{
"quantity": "2",
"sub_total": "120000",
"order_id": "1",
"product_id": "3"
},
{
"quantity": "1",
"sub_total": "120000",
"order_id": "1",
"product_id": "2"
}
]
}
Currently, i'm using a form request to validate, let say this is my Form Request App\Http\Requests\OrderDetailRequest
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;
public function rules()
{
return [
'details.*.quantity' => 'required',
'details.*.sub_total' => 'required',
'details.*.order_id' => 'required',
'details.*.product_id' => 'required',
];
}
public function messages()
{
return [
'details.*.quantity.required' => 'Quantity is required',
'details.*.sub_total.required' => 'Sub Total is required',
'details.*.order_id.required' => 'Order Id is required',
'details.*.product_id.required' => 'Product is required',
];
}
protected function failedValidation(Validator $validator)
{
throw new HttpResponseException(response()->json($validator->errors(), 422));
}
I have read few article saying that using a FormRequest is the best practice to validate a request, instead of putting validation in the controller.
So my case is: When i try to validate (e.g leave quantity value as null), i got no error message. it just gave me [] an empty array
from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2NYMiy1
via IFTTT
Aucun commentaire:
Enregistrer un commentaire