Extending core symfony Form Bootstrap Theme

548 Views Asked by At

Symfony provides two bootstrap form themes (amongst others) which define "checkbox_row" blocks.

I attempted to use those in my twig templates, only resulting in the following error:

The function "checkbox_row" does not exist in CompanyBundle:OrderingRule:new.html.twig at line 17

In my config.yml file I have set:

form:
    resources: ['bootstrap_3_layout.html.twig']

Here's my twig

{{ form_start(form, {'attr': {'novalidate': 'novalidate'}, 'action': path('ordering_rule_create'), 'method': 'POST'}) }}
      <div class="row">
        <div class="col-lg-3">
          <p class="small">Step 1 of 4:</p>
          <p>Order Types</p>
        </div>
        <div class="col-lg-9">
          {{ checkbox_row(form.isMealsIncluded) }}
          {{ form_row(form.isCateringIncluded, {'label': 'Catering'}) }}
          {{ form_row(form.billingType, {'label': null}) }}
        </div>
      </div>
      <div class="row">
        <div class="col-lg-3">
          <p class="small">Step 2 of 4:</p>
          <p>Applicable To</p>
        </div>
        <div class="col-lg-9">
          {{ form_row(form.applicableType) }}
          {{ form_row(form.companyLocations) }}
          {{ form_row(form.companyDepartments) }}
        </div>
      </div>
      <div class="row">
        <div class="col-lg-3">
          <p class="small">Step 3 of 4:</p>
          <p>Select days and time<br>when users can order</p>
        </div>
        <div class="col-lg-9">
          {{ form_row(form.applicableWeekdays) }}
          {{ form_row(form.applicableTimeFrom) }}
          {{ form_row(form.applicableTimeTo) }}
        </div>
      </div>
      <div class="row">
        <div class="col-lg-3">
          <p class="small">Step 4 of 4:</p>
          <p>Budget</p>
        </div>
        <div class="col-lg-9">
          {{ form_row(form.budget) }}
        </div>
      </div>
      {{ form_end(form) }}
1

There are 1 best solutions below

8
On

You are confusing form fragments with means of rendering a form

{% block checkbox_row -%}
    <div class="form-group{% if not valid %} has-error{% endif %}">
        {{- form_widget(form) -}}
        {{- form_errors(form) -}}
    </div>
{%- endblock checkbox_row %}

This is how the checkbox row is render as a fragment in bootstrap_3_layout.html.twig

But you can't use the function like this.

You can override it by defining the block in your template:

{% block checkbox_row -%}
    {# Do whatever you want #}
{%- endblock checkbox_row %}

EDIT

How to render a form field:

{{ form_row(form.field) }} 

or

{{ form_label(form.field) }}
{{ form_errors(form.field) }}
{{ form_widget(form.field) }}

this is how you can render a field, there are no other way. You can still render the form globally by {{form(form)}}

But the existence of the fragment checkbox_rowis intended to allow you a good way of rendering customization