Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array upload validation & bugs #13779

Closed
ctf0 opened this issue May 30, 2016 · 4 comments
Closed

Array upload validation & bugs #13779

ctf0 opened this issue May 30, 2016 · 4 comments

Comments

@ctf0
Copy link

ctf0 commented May 30, 2016

OSX 10.11.5
brew PHP 7.0.7
Lara v5.2.34

when i try to upload multiple files i get an error,

Allowed memory size of 134217728 bytes exhausted (tried to allocate 16384 bytes)

which come up from time to time and the solution would be adding ini_set('memory_limit', '500M'); in the routes.php file.

the other issue is related more to the validation it self
$rules = [
            'title'       => 'required|unique:posts|max:255',
            'description' => 'required',
            'photo.*'     => 'required|mimes:jpg,jpeg,png,bmp|max:4096'
        ];

 $validate = Validator::make($request->all(), $rules);
 $photos   = $request->file('photo');

        if ($photos[0] != null) {
            $validate->after(function ($validate) use ($photos) {
                foreach ($photos as $photo) {
                   // this solution was working since L4.2 to get the exact error instead of just getting required or empty error array in the redirect->back();
                  // it was working till the prev release v5.1.36
                    if ($photo->getError() != 0) {
                        $validate->errors()->add('photo_general_err', $photo->getErrorMessage());
                    }
                }
            });
        }

        if ($validate->fails()) {
            dd($validate);
            throw new ValidationException(
                $validate,
                redirect()->back()
                          ->withInput($request->input())
                          ->withErrors($validate->errors()->getMessages())
            );
        }

and in the view 'btw the docs doesnt have how to display validated array errors, it only show how to write the validation logic"

<strong>{{ $errors->first('photo.0') }}</strong>

which doesnt work anymore because the returned array doesnt always return the item photo.0 but have photo.1/2/etc... so i cant view those errors anymore

maybe we can have something like an alias for the array "it could be the same name of the array ex.photo" so we can loop over it ?

and here is the returned validation

Validator {#317 ▼
  #translator: Translator {#104 ▶}
  #presenceVerifier: DatabasePresenceVerifier {#203 ▶}
  #container: Application {#3 ▶}
  #failedRules: array:1 [▶]
  #messages: MessageBag {#311 ▼
    #messages: array:6 [▼
      "photo.2" => array:2 [▼
        0 => "photo must be a file of type: jpg, jpeg, png, bmp."
        1 => "photo should not be more than 4096."
      ]
      "title" => array:1 [▶]
      "description" => array:1 [▶]
    ]
    #format: ":message"
  }
  #data: array:4 [▼
    "_token" => "VhRk3PJ1ZgQfeIfLILJRvU0Q7u1C23pjPbH126Ll"
    "title" => "aksjhdkjsahd"
    "description" => "kajhdkjahdakjdh"
    "photo" => array:3 [▼
      0 => UploadedFile {#304 ▼
        -test: false
        -originalName: "01_homepage_02.jpg"
        -mimeType: "image/jpeg"
        -size: 4091387
        -error: 0
        path: "/private/var/folders/l9/3jcqh0d111nfg9469_h0fbyw0000gn/T"
        filename: "phpYLKyWt"
        basename: "phpYLKyWt"
        pathname: "/private/var/folders/l9/3jcqh0d111nfg9469_h0fbyw0000gn/T/phpYLKyWt"
        extension: ""
        realPath: "/private/var/folders/l9/3jcqh0d111nfg9469_h0fbyw0000gn/T/phpYLKyWt"
        aTime: 2016-05-30 14:58:31
        mTime: 2016-05-30 14:58:31
        cTime: 2016-05-30 14:58:31
        inode: 6293403
        size: 4091387
        perms: 0100600
        owner: 501
        group: 20
        type: "file"
        writable: true
        readable: true
        executable: false
        file: true
        dir: false
        link: false
      }
      1 => UploadedFile {#313 ▼
        -test: false
        -originalName: "01_Main.png"
        -mimeType: "image/png"
        -size: 684994
        -error: 0
        path: "/private/var/folders/l9/3jcqh0d111nfg9469_h0fbyw0000gn/T"
        filename: "phpTLZ0Le"
        basename: "phpTLZ0Le"
        pathname: "/private/var/folders/l9/3jcqh0d111nfg9469_h0fbyw0000gn/T/phpTLZ0Le"
        extension: ""
        realPath: "/private/var/folders/l9/3jcqh0d111nfg9469_h0fbyw0000gn/T/phpTLZ0Le"
        aTime: 2016-05-30 14:58:31
        mTime: 2016-05-30 14:58:31
        cTime: 2016-05-30 14:58:31
        inode: 6293404
        size: 684994
        perms: 0100600
        owner: 501
        group: 20
        type: "file"
        writable: true
        readable: true
        executable: false
        file: true
        dir: false
        link: false
      }
      2 => UploadedFile {#305 ▼
        -test: false
        -originalName: "01_shot.gif"
        -mimeType: "image/gif"
        -size: 7138753
        -error: 0
        path: "/private/var/folders/l9/3jcqh0d111nfg9469_h0fbyw0000gn/T"
        filename: "phpb0VHf3"
        basename: "phpb0VHf3"
        pathname: "/private/var/folders/l9/3jcqh0d111nfg9469_h0fbyw0000gn/T/phpb0VHf3"
        extension: ""
        realPath: "/private/var/folders/l9/3jcqh0d111nfg9469_h0fbyw0000gn/T/phpb0VHf3"
        aTime: 2016-05-30 14:58:31
        mTime: 2016-05-30 14:58:31
        cTime: 2016-05-30 14:58:31
        inode: 6293405
        size: 7138753
        perms: 0100600
        owner: 501
        group: 20
        type: "file"
        writable: true
        readable: true
        executable: false
        file: true
        dir: false
        link: false
      }
    ]
  ]
  #files: array:3 [▶]
  #initialRules: array:3 [▶]
  #rules: array:5 [▶]
  #implicitAttributes: array:1 [▶]
  #after: array:1 [▶]
  #customMessages: []
  #fallbackMessages: array:2 [▶]
  #customAttributes: []
  #customValues: []
  #extensions: array:2 [▶]
  #replacers: []
  #sizeRules: array:4 [▶]
  #numericRules: array:2 [▶]
  #implicitRules: array:10 [▶]
  #dependentRules: array:12 [▶]
}

also as u can see the each photo now has error of 0 , where b4 it used to have numbers the correspond to the error status which was making sense.

#12118

@ctf0
Copy link
Author

ctf0 commented Jun 5, 2016

i found a solution-ish for the array errors not showing up

$rules = [
            'title'       => 'required|unique:posts|max:255',
            'description' => 'required',
            'photo.*'     => 'required|mimes:jpg,jpeg,png,bmp|max:4096'
        ];

 $validate = Validator::make($request->all(), $rules);

        if ($validate->fails()) {
            foreach ($validate->errors()->getMessages() as $k => $v) {
                // we search for any error that has "word.number"
                $last= [];
                if (preg_match("/\w+\.\d+/", $k)) {
                    // save into new array
                    $last[] = $v;
                }
            }
            // flatten the array into one level
            $res = array_flatten($last);
            $validate->errors()->add('photo_err', $res);

            throw new ValidationException(
                $validate,
                redirect()->back()
                          ->withInput($request->input())
                          ->withErrors($validate->errors()->getMessages())
            );
        }

now the photo_err became and array of arrays so we need to fix that or the view will keep giving the fockin

Array to string conversion

so make a presenter

 public function test($array)
    {
        $array['photo_err'] = call_user_func_array('array_merge', $array['photo_err']);
        return $array['photo_err'];
    }

and in the view

@inject('test','The-Class-name')

@if ($errors->getMessages())
    @foreach ($test->test($errors->getMessages()) as $one)
        <li>{{ $one }}</li>
    @endforeach
@endif

@ctf0
Copy link
Author

ctf0 commented Jun 7, 2016

update:
it seems the memory issue is related mostly to intervention "not 100% sure" but maybe this could help.
Intervention/image#567

@themsaid
Copy link
Member

themsaid commented Jun 7, 2016

I think the memory issue is not laravel related, for the other problem you have I don't seem to get it. Can you please share more details with a sample code?

Better, can you please write a failing test?

@ctf0
Copy link
Author

ctf0 commented Jun 7, 2016

@themsaid currently when u validate an array how do u retrive the errors of the failed files ? , cuz the old file.0 wont work if ur failed item is file.1 or 2 or etc..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants