0
votes
//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();
}

enter image description here

enter image description here

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

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

API will be used by the android APP. How I can solve this? How I can have both validation on text and file inputs?

3

3 Answers

0
votes

Using application/x-www-form-urlencoded is the correct way to upload images. According to your second screenshot, you are not sending the file field in Postman, but you are sending it in the first screenshot.

0
votes

see this

'name' => 'string|required|min:5',

minimum is 5 character but you send test or 4 chars. Laravel validation rule, if it failed it will stop or next validation will not checked.

0
votes

I think I've found a solution. Changing method from PUT to POST seems to fix this issue.