javierojeda
javierojeda

Reputation: 309

Lumen can't validate array of files

I created a webservice using Lumen 5.7.

I'm sending an array of files but I can't validate it using the common-known method for validating arrays on Laravel/Lumen:

public function create(Request $request) {
    $this->validate($request, [
        'phone_number' => 'required',
        'latitude' => 'required',
        'longitude' => 'required',
        'status' => 'required', Rule::in(['pre','authorized','archived']),
        'photos' => 'required',
        'photos.*' => 'mimes:jpg,jpeg,png,bmp'
    ]);
}

It seems to ignore that photos rules that I defined there. If i do a d($request->all()) I got:

enter image description here

Which means the files reach the server, but they can't be validated.

Here is the html code I'm using to upload the files:

<!DOCTYPE html>
<html lang="en" dir="ltr">
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <form class="" action="terrains" method="post" enctype="multipart/form-data">
            <input type="file" name="photos[]" value="" multiple>
            <input type="submit" name="" value="Send">
        </form>
    </body>
</html>

I've also tested it using Postman:

enter image description here

As you can see, the other validations are being executed correctly except for those of the photos.

I need validations because of several reasons:

Upvotes: 0

Views: 1117

Answers (2)

Chukwuemeka Inya
Chukwuemeka Inya

Reputation: 2671

I will only comment on the new fields I added since you already understand what the other fields do. So, make the following edit.

Your form:

<form class="" action="terrains" method="post" enctype="multipart/form-data">
    <input type="text" name="phone_number" placeholder="Phone Number">
    <input type="text" name="longitude" placeholder="Longitude">
    <input type="text" name="latitude" placeholder="Latitude">
    <input type="text" name="status" placeholder="Status">
    <input type="file" name="photos[]" value="" multiple>
    <input type="submit" name="" value="Send">
</form>

Validation:

public function create(Request $request) {
    $this->validate($request, [
        'phone_number' => 'required',   // required translates to the fact that the field must not be empty.
        'latitude' => 'required',
        'longitude' => 'required',
        'status' => 'required', Rule::in(['pre','authorized','archived']),
        'photos' => 'required',
        'photos.*' => 'image|max:5000|mimes:jpg,jpeg,png,bmp'    // image = Must be an image, max = The image size must not be bigger than the specified size (5MB)
    ]);
}

Upvotes: 1

Gurpal singh
Gurpal singh

Reputation: 1549

It should be like this

public function create(Request $request) {
$this->validate($request, [
    'phone_number' => 'required',
    'latitude' => 'required',
    'longitude' => 'required',
    'status' => 'required', Rule::in(['pre','authorized','archived']),
    'photos.*' => 'required|mimes:jpg,jpeg,png,bmp'
]);

}

Upvotes: 0

Related Questions