Framework: File validation returns no error on PHP 7

Created on 17 Feb 2016  Â·  17Comments  Â·  Source: laravel/framework

Because the issue seems to be abandoned after it's closed, I decided to create a new one.
https://github.com/laravel/framework/issues/12118

After doing several testing, I conclude that the problem appears in PHP 7, it's working fine on PHP 5.

Please use Homestead for testing.

bug

Most helpful comment

I just tried it, still not working. The temporary fix also need to be modified, before it returned single empty object, now it returns 3 null object. (array 2 and 4 are filled with file)

array:3 [â–¼
  0 => null
  1 => null
  3 => null
]

The new temporary fix

    public function withInput(array $input = null)
    {
        $input = $input ?: $this->request->input();

        $this->session->flashInput($data = array_filter($input, $callback = function (&$value) use (&$callback) {
            if (is_array($value)) {
                $value = array_filter($value, $callback);
                return false;
            }

            return ! $value instanceof SymfonyUploadedFile;
        }));

        return $this;
    }

All 17 comments

Could you add a test case that fails please?

I experience the same issue running php 7. Will debug and update shortly.

Edit: The validation errors are the exact same per PHP version, but calling the errors message bag in my views on PHP 7 using $errors->has('files.0') returns false only on PHP 7.

This bug only occurs when you're validating multiple files. This issue does not occur when you're only validating a single file upload.

How to Reproduce

Create a form request:

class FileRequest extends Request
{
    /**
     * The issue validation rules.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'files.*' => 'mimes:doc,docx,xls,xlsx,png,jpg,bmp,pdf',
        ];
    }
}

DI it into a controller method:

class FileController extends Controller
{
    use ValidatesRequests;

    public function create()
    {
        return view('form');    
    }

    public function store(FileRequest $request)
    {
            //
    }
}

Create your form:

<form method="POST" action="{{ route('files.store') }}" accept-charset="UTF-8" enctype="multipart/form-data">

<input class="form-control" multiple="1" name="files[]" type="file" id="files[]">

<input type="submit">

</form>

Select an invalid file Inside the form, and then inside your form.blade.php view, edit it to:

{{ dd($errors->has('files.0')) }} // returns false

Dumping the $errors bag in PHP 5.6:

ViewErrorBag {#1020 â–¼
  #bags: array:1 [â–¼
    "default" => MessageBag {#1019 â–¼
      #messages: array:1 [â–¼
        "files" => array:1 [â–¼
          0 => "The files.0 must be a file of type: doc, docx, xls, xlsx, png, jpg, bmp, pdf."
        ]
      ]
      #format: ":message"
    }
  ]
}

Dumping the $errors bag in PHP 7

ViewErrorBag {#1023 â–¼
    #bags: []
}

More Information

What's even more strange in PHP 7, is when I submit the form while dumping the errors in PHP 5.6, and switch PHP versions on the fly to PHP 7, and re-submit the form, I receive the correct error bag instance.

I'm running Windows 10 64-bit with IIS 10 if that helps at all.

@stevebauman
Thank you for providing through test.

@GrahamCampbell
I already gave the info on the other issue and stevebauman above also already gave his test but I'll just paste my comment from the other issue here.

I just tested using the fresh 5.2.14, there's still no error messages. So why is this issue closed?

Here's the app and resources folder, you just need to replace them to test. Just empty the name field and upload image other than gif.
http://s000.tinyupload.com/index.php?file_id=71431977644503943832

Tested using xampp and homestead

Temporary fix:
Replace the withInput method in the Illuminate\Http\RedirectResponse with this (added return false)

    public function withInput(array $input = null)
    {
        $input = $input ?: $this->request->input();

        $this->session->flashInput($data = array_filter($input, $callback = function (&$value) use (&$callback) {
            if (is_array($value)) {
                $value = array_filter($value, $callback);

                if (empty($value))
                return false;
            }

            return ! $value instanceof UploadedFile;
        }));

        return $this;
    }

I'm running Windows 10 64 bit

What about 5.2.19?

I just tried it, still not working. The temporary fix also need to be modified, before it returned single empty object, now it returns 3 null object. (array 2 and 4 are filled with file)

array:3 [â–¼
  0 => null
  1 => null
  3 => null
]

The new temporary fix

    public function withInput(array $input = null)
    {
        $input = $input ?: $this->request->input();

        $this->session->flashInput($data = array_filter($input, $callback = function (&$value) use (&$callback) {
            if (is_array($value)) {
                $value = array_filter($value, $callback);
                return false;
            }

            return ! $value instanceof SymfonyUploadedFile;
        }));

        return $this;
    }

It seems not fixed yet.
I'm using L 5.2.27 and got this problem with Homestead and PHP 7

I did a fresh update on 5.2.*.
After inspecting the RouteServiceProvider I saw that the web middleware group is included by default.
Removing the web middleware from my own routes resolved this issue. Seems like it gets actioned twice which causes the message bags to freak out.

Closing since this is a user error caused by adding middlewares more than once.

I didn't add any middlewares except "auth" to my routes and have the same issue. Temporary solution from @lino10 helps me, but I don't want to edit framework files after each update.

I used the Auth scaffolding. It injected Route:auth(); to my routes, which sat outside of my route group. I noticed the errors were showing on the Auth pages, but not my own. That's when I saw the RouteServiceProvider included web by default. Using 5.2.29 I don't get any errors including auth middleware on homestead with php7.

@GrahamCampbell is this issue already fixed?

@lino10 it seems to be fixed in 5.2.40

@xpert13 Have you tried it?
From what I see it they're still debating whether it's bug or not..

@lino10 Yes, I did. It works fine for me now.

@xpert13 Hi, I just tested it. It works now. Thanks for notifying me! :)

@3alampro Are you the one providing the patch? Thanks for providing the patch :)

@lino10 yes i did provide working patch but @taylorotwell provided a better and clean solution for the bug 10 days ago here
https://github.com/laravel/framework/commit/85249beed1e4512d71f7ae52474b9a59a80381d2

Hi this is still an issue on the 5.1 branch.

I've tested it and the @taylorotwell fix from https://github.com/laravel/framework/commit/85249beed1e4512d71f7ae52474b9a59a80381d2 works fine on the 5.1 branch.

Is it possible to update the RedirectResponse class with those changes in the 5.1 branch as well?

Was this page helpful?
0 / 5 - 0 ratings