grav-plugin-form icon indicating copy to clipboard operation
grav-plugin-form copied to clipboard

File upload without AJAX

Open fredrikekelund opened this issue 9 years ago • 6 comments

I feel a bit like a party pooper with this, but I'm looking to create a form with a custom template that includes a simple file input. I don't really want to upload the file with AJAX, or use the custom dropzone design included in the default form template. It's nice functionality, but it seems targeted at a pretty specific use case, whereas I just want a click-to-submit form. From looking at 4f0d5b66166db79018ca16cdf07d8e83888d55ec, it seems like the form won't pick up any files from POST submissions anymore. Is that the case? Couldn't these two use cases coexist?

fredrikekelund avatar Oct 04 '16 12:10 fredrikekelund

Closing because due to issue age. Please re-open if you continue to have issues

rhukster avatar May 12 '18 16:05 rhukster

I also would like to know, if form submission would be possible without additional javascript. That'd be nice.

NicoHood avatar Jan 10 '21 17:01 NicoHood

I think creating a simple field for file upload should work out of the box with the latest versions. Try it out and report a new bug if that doesn't work.

mahagr avatar Jan 11 '21 09:01 mahagr

I tried the following without success (it requires javascript):

custom_file:
    name: myfile
    type: file
    label: A Label
    destination: 'user/data/uploads/myupload'
    multiple: false
    autofocus: false
    accept:
      - image/*

Could you please reopen the issue, as the name and description is exactly what the issue is about?

NicoHood avatar Jan 11 '21 19:01 NicoHood

I meant you need to create your own file field type -- with plain old HTML file field. I think that should actually work as long as you define it properly.

mahagr avatar Jan 12 '21 11:01 mahagr

Now I tried with:

custom_file:
    name: myfile
    type: simplefile
    label: A Label
    destination: 'user/data/uploads'
    multiple: false
    autofocus: false
    accept:
      - image/*

simplefile.html.twig

{% extends "forms/field.html.twig" %}

{% set defaults = config.plugins.form %}
{% set files = defaults.files|merge(field|default([])) %}

{% block input %}
    <input
            {# required attribute structures #}
            {% block input_attributes %}
                type="file"
                {% if files.multiple %}multiple="multiple"{% endif %}
                {% if files.accept %}accept="{{ files.accept|join(',') }}"{% endif %}
                {% if field.disabled %}disabled="disabled"{% endif %}
                {% if field.random_name %}random="true"{% endif %}
                {% if required %}required="required"{% endif %}
                {{ parent() }}
            {% endblock %}
    />
{% endblock %}

Still not working. I tried to understand the code, but it seems to be split in many files with a lot of legacy stuff. Is the "legacy" upload the "normal" html upload you are referring to or is this "just" legacy code?

I want to note that I verified that file via ajax still works.

NicoHood avatar Jan 12 '21 17:01 NicoHood