5

In my PhotosController, I am trying to modify the edit action so that it shows the existing value in the Form::file() input field (and, so that it repopulates the field if validation fails).

if ($validation->passes())
{
        // saves the image on the FS and updates the db entry
}

    return Redirect::route('photos.edit')
        ->withInput(Input::all())
        ->withErrors($validation)
        ->with('message', 'There were validation errors.');

The uploading process works fine, but when I view an existing record, or if validation fails upon creation, the Form::file doesn't show the value. I looked up the method in the api, and it only accepts the name and optional options arrays. Whereas, the other methods in the FormBuilder class allow us to set the value using Input::old().

I've tried passing in the following values to withInput, but they've been to no avail: * Input::all() * Input::old() * Input::except('photo_path') * Input::get('photo_path')

How can I update this so that if validation fails, or if a user views an existing record, the Form::file() method will show the existing value?

I'm sure that I'm overlooking something incredibly simple because I haven't found other threads of people asking this...

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
DroBuddy
  • 303
  • 1
  • 6
  • 16

4 Answers4

8

Apparently you can't. Browsers don't allow you to set a value on a file input for security reasons.

There are two silimar questions, not laravel related but i think answers could help you:

Can you re-populate file inputs after failed form submission with PHP or JavaScript?

Restoring the value of a input type=file after failed validation

Community
  • 1
  • 1
Needpoule
  • 4,476
  • 24
  • 33
  • Ah, well that does make sense. Thanks for enlightening me. ;) I'll use JavaScript... This should solve both issues; validation, and updating records. By using JS, I'll be able to update the `select option` with the current value from the DB so that `Form::file()` gets set onLoad. Thanks for pointing me in the right direction. – DroBuddy Mar 28 '14 at 00:42
2

Maybe your best shot would be to make the validation client-side (javascript/jquery).

Of course there has to be (ALWAYS) server-side validation. (remember javascript can be disabled on client-side).

But I believe that, this way 99.99% of the cases, the form submission will be successful!

Basically, when is indeed submit, it is already validated with sames rules as the servers, so it will not submit until it is not "as the server likes" :)

Cheers

ijpatricio
  • 170
  • 7
0

For complex form submits I would recommend to use AJAX:

  1. You may do a first AJAX form send that does the server side validation.
  2. Then, if it's all fine you should launch via Javascript the form POST with file input.

I've answered a similar question for Laravel there but I hope it will be helpful for a general approach: Laravel 5.1: keep uploaded file as old input

XaviQV
  • 185
  • 2
  • 8
-1

Remove the redirect, your validation and input values should show on the same page, only redirect upon success.

fire
  • 21,383
  • 17
  • 79
  • 114