简体   繁体   中英

Symfony2 twig override submit block under custom block

I've made a custom Form Type with this twig template associated:

{% block date_filter_widget %}
  {% spaceless %}
    <div class="input-daterange input-group js-datepicker">
        {{ form_widget(form.fromDate) }}
        <span class="input-group-addon">to</span>
        {{ form_widget(form.toDate) }}
        OVERRIDE THE SUBMIT BLOCK HERE
    </div>
   {% endspaceless %}
{% endblock %}

I would like to override the template for the submit button of this custom field/block.

I've tried putting the block submit within this block , but in this way ANY submit block gets overridden (even if they are not of date_filter_widget ). Another way is just to create the submit button manually (and pass the attributes) but in this way I am not truly overriding the submit button of the form (so I end up with other issues).

I've seen also a solution suggesting an if/else in the block submit checking whether the widget prefix matches my custom type name but it seems a bit an hack more then a proper override.

In that block I've add {{ block('date_submit_widget') }}

Then made a custom submit block like:

{%- block date_submit_widget -%}
{%- set type = type|default('submit') -%}
{% set attr = attr|merge({class: (attr.class|default('') ~ ' btn btn-primary')|trim}) %}
<span class="input-group-btn">
    <button type="{{ type|default('button') }}" name="{{full_name}}_filter" {{ block('button_attributes') }}>
        <span class="glyphicon glyphicon-filter"></span>
    </button>
</span>
{%- endblock date_submit_widget -%}

The important part I was missing is that the <button> needs a name attribute and it should not stay in the same array of the other inputs field otherwise validation will fail

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM