Ok I think I've found a problem. I was sending this request by a PUT. Somehow, when I've changed a method to the POST, it works. Not really sure why...
Dec 8, 2018
7
Level 1
Why laravel doesn't validate 'form-data' request?
//TestRequest.php
public function rules()
{
return [
'name' => 'string|required|min:5',
'tip' => 'string|required|min:5',
'answer' => 'string|required',
'image' => 'file|required|mimes:png,jpg,jpeg'
];
}
//TestController.php
public function put(TestRequest $request)
{
$validated = $request->validated();
}
I'm doing some rest API. I need a form with some text fields and one image upload field but I have a problem with validating it.
- When I'm sending the request as 'form-data' in the Postman, Laravel doesn't see in the validation any fields (why?).
Response none of fields validated even if it's send to Laravel
{
"message": "The given data was invalid.",
"errors": {
"name": [
"The name field is required."
],
"tip": [
"The tip field is required."
],
"answer": [
"The answer field is required."
],
"image": [
"The image field is required."
]
}
}
- When I'm sending the request as application/x-www-form-urlencoded Laravel sees my text fields, but I can't, of course, upload the image. (I don't see how to upload image that way in Postman. I think it's not possible)
Response - name, tip and answer field validated correctly, but I can't add "image"
{
"message": "The given data was invalid.",
"errors": {
"image": [
"The image field is required."
]
}
}
API will be used by the android APP. How I can solve this? How I can have both validation on text and file inputs?
Level 1
3 likes
Please or to participate in this conversation.