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);
}