4

Note:

The answer(s) below reflect the state of legacy browsers in 2009. Now you can actually set the value of the file input element via JavaScript in 2017.

See the answer in this question for details as well as a demo:
How to set file input value programatically (i.e.: when drag-dropping files)?

I know this question is duplicated but I think maybe someone can help me.

Let me ask first the problem.

I have a form to update some fields: name, order, public, pathheader and pathhome and my question is this:

It's possible to update the form, with the same value in pathheader and pathhome without clicking in the input type file again?

The input type file looks like here:

@if (Storage::disk('projects')->has($project->slug))
    <img src="{{ asset('/storage/projects/'.$project->slug.'/header.png') }}" id="img" class="img" style="width:100%;height:200px;background-color:#ccc;border:2px solid gray;">
@else
    <img src="" id="img" class="img" style="width:100%;height:200px;background-color:#ccc;border:2px solid gray;">
@endif
<input type="file" name="pathheader" id="pathheader"  class="form-control-file" aria-describedby="fileHelp" style="display:none;">

So when I render the view, it shows the images I thinks it's not practical to find it in server and click it again, if you don't want to change it.

Let me know your opinions, and if someone know how to do it i will be appreciated. (If know how to do it with some library or other functionalities let me know it.

Thanks a lot.

SOLVED

Easier way: Make data not required in controller.

Inside the function:

public function updateProject(Request $request, $id) 
{
    $this->validate($request, array(
        'slug'=> 'required',
        'order'=> 'required',
        'public'=> 'required'
    ));
    $project = Project::find($id);
    $project->slug = $request->input('slug');
    $project->order = $request->input('order');
    $project->public = $request->input('public');
    if ($request->hasFile('pathheader')){
        $project->pathheader = $request->file('pathheader');
        \Storage::disk('projects')->putFileAs($project->slug,$project->pathheader,'header.png');
    }
    if ($request->hasFile('pathhome')){
        $project->pathhome = $request->file('pathhome');
        \Storage::disk('projects')->putFileAs($project->slug,$project->pathhome,'home.png');

    }
    $project->save();
    return redirect()->route('admin.projects.show', $project->id);
}
Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Lluís Puig Ferrer
  • 1,128
  • 7
  • 19
  • 49
  • I hope you have already seen this: https://stackoverflow.com/q/1696877/5894241 – Nisarg Shah Aug 07 '17 at 09:52
  • You can't update form. You update __record__ in a database. And if value for some field is __not provided__ - there's no need to update this field. – u_mulder Aug 07 '17 at 09:58
  • Hi @NisargShah first of all thanks for your fast answer, I already seen this. And I see maybe the better way is to make with JS an instant autoupload of the file. What do you think, it's the best way? – Lluís Puig Ferrer Aug 07 '17 at 09:58
  • Hi @u_mulder first of all thanks for your fast answer, It's true, maybe the easier way is make in controller not required path, and just render it. I will try this, if I have any question can I ask you? If you can, put your comment like an answer, it will be marked ;). – Lluís Puig Ferrer Aug 07 '17 at 10:00
  • 1
    I am confused with what you want to achieve – Romnick Susa Aug 07 '17 at 10:01
  • You can't do an `instant autoupload`. Without user interaction, nothing will happen with your fileinput – baao Aug 07 '17 at 10:17

1 Answers1

4

Due to browser's security, You cannot just put default value to file input.

The better way is to check if the user selected a file before updating your records.

So on your update function:

public function update(){
    // Make sure you didn't required the user to select file.
    $attribute = [
        'name' => $request->name,
        'order' => $request->order
    ]
    if($request->hasFile('pathheader')){
        //If user select a file, upload the file 
        //Then you should update your record by 
        //adding fields to your update attribute. 
        $attribute['pathheader'] => $pathheader;

    }
    //otherwise, no changes will happen to your 'pathheader' column.
    DB::table('yourtable')->where('id',$id)->update($attribute);
}
Romnick Susa
  • 1,279
  • 13
  • 31
  • Thanks a lot @RomnickSusa i always try to make it harder. The easier way is yours, i'm sure. It was successfully. I will edit my question with the new function in controller, you can check it and if you think it can be more efficient just put a comment. Anyway, thanks for your answer:) – Lluís Puig Ferrer Aug 07 '17 at 10:32
  • Okay, update your question then ill update my answer. It would be better if you provide your controller's code. – Romnick Susa Aug 07 '17 at 10:36
  • Question updated – Lluís Puig Ferrer Aug 07 '17 at 10:39