rkoziol's avatar

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.

  1. 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."
        ]
    }
}

  1. 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?

0 likes
7 replies
rkoziol's avatar
rkoziol
OP
Best Answer
Level 1

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...

3 likes
nmeri17's avatar

@rkoziol cuz it's a crap framework. I just logged in to tell you how grateful I am to read your answer

kayatech's avatar

Hi

now i'm get same problem

public function put(TestRequest $request) { $validated = $request->validated(); }

how to use this...?

rkoziol's avatar

Changing method name won't help. Change request method in your routing.

mena_devs's avatar

Hey guys, I know it's been a long time, but possibly someone needs it.

Postman will only upload the file when the HTTP Request is "POST", which will then allow Laravel to see it. You could always retrieve your uploaded file from the request using

$request->file('$your_file_name');

Let me know how it goes 😁

toby5box's avatar

Had the same problem in Laravel 8 and using PUT method from frontend with multipart/form-data content type. It doesn't work. The fields are all in the submitted body but validation doesn't see them.

The solution in my case was to switch to method POST from frontend, and:

        formData.append('_method', 'PUT');

The route was a resource route with update enabled. php artisan route:list showed that the PUT method was routed. Spent a while figuring this out...

Please or to participate in this conversation.