Skip to content

fileForm

A self-contained upload form: a label with a hidden file input that submits its own form as soon as a file is chosen. Typical use is an avatar or a quick import button next to other content.

Lte3::fileForm(string $name, array $attrs = [])
{!! Lte3::fileForm('avatar', [
'url_save' => route('admin.users.avatar', $user),
'label' => 'Upload avatar',
'accept' => 'image/*',
'html' => '<img src="'.e($user->avatar_url).'" style="width: 100px;">',
]) !!}

Clicking the label (the html and the bold label text) opens the file dialog; after a file is chosen the form is submitted with a full page request.

AttrTypeDefaultDescription
url_savestring-Form action.
actionstring''Form action when url_save is not set.
methodstringPOSTForm method. Anything other than GET is sent as POST with _method (method spoofing).
multipleboolfalsemultiple on the input, the name gets [].
htmlstring-HTML printed inside the label before the text, e.g. the current image.
labelstringStr::studly($name)Bold label text. '' hides it.
classstring-Classes of the <form> and of the file input.
disabledboolfalseAdds disabled to the file input.

Keys from field_attrs (accept, id, title, …) are printed on the file input.

The form is opened with Lte3::formOpen(), so it gets enctype="multipart/form-data", _token, _method and the hidden fields of Lte3::formHiddenUsing() (see Forms). It is display: inline-block.

FieldValue
avatarUploadedFile (or avatar[] with multiple)
_token, _methodform service fields
public function avatar(Request $request, User $user)
{
$request->validate(['avatar' => ['required', 'image', 'max:5120']]);
$user->update(['avatar' => $request->file('avatar')->store('avatars', 'public')]);
return back()->with('success', 'Avatar updated');
}

main.js submits the closest form on change of any .js-form-submit-file-changed input[type="file"] (a delegated handler). The same class works on any form of your own:

{!! Lte3::formOpen(['action' => route('admin.import'), 'files' => true, 'class' => 'js-form-submit-file-changed']) !!}
<label class="btn btn-primary"><input type="file" name="file" hidden> Import</label>
{!! Lte3::formClose() !!}

See also file for a file input inside a regular form.