6

I have the a form twig template, where I want to parse a specific fields help text with the raw filter (it contains html). The field is called postcode in a form called Clinic

According to here http://symfony.com/doc/current/cookbook/form/form_customization.html#how-to-customize-an-individual-field

Form template:

{% extends 'AgriHealthAhpBundle::admin.html.twig' %}
{% form_theme form 'AgriHealthAhpBundle:Form:fields.html.twig' %}

{% block _clinic_postcode_row %}
        <div class="row">
            test<div class="small-12 medium-3 columns label">{{ form_label(form) }}</div>
            <div class="small-12 medium-6 columns widget">
                {{ form_widget(form) }}
                <div class="error">
                    {{ form_errors(form) }}
                </div>
            </div>
            <div class="small-12 medium-3 columns help">
                {% if help is defined %}
                    {{ help|raw }}
                {% endif %}
            </div>
        </div>
{% endblock %}

{% block admin -%}
    <h1>New Clinic</h1>

    {{ form(form) }}

    <div class="row form_actions">
        <div class="small-12 medium-offset-3 medium-2 columns submit">
            <button type="submit" id="agrihealth_ahpbundle_clinic_submit_visible" name="agrihealth_ahpbundle_clinic[submit]">Create</button>
        </div>
        <script type="text/javascript">
            jQuery(document).ready(function() {
                jQuery('#agrihealth_ahpbundle_clinic_submit_visible').click(function(){
                    jQuery('form[name="agrihealth_ahpbundle_clinic"]').submit();
                });
            });
        </script>
        <div class="small-12 medium-2 columns cancel">
            <a href="{{ path('clinic') }}">
                Cancel
            </a>
        </div>
        <div class="small-12 medium-2 end columns cancel">
            <a href="{{ path('clinic') }}">
                Back to List
            </a>
        </div>
    </div>
{% endblock %}

AhpBundle/Resources/views/Form/fields.html.twig

{% block form_row %}
    {% spaceless %}
    <div class="row">
        <div class="small-12 medium-3 columns label">{{ form_label(form) }}</div>
        <div class="small-12 medium-6 columns widget">
            {{ form_widget(form) }}
            <div class="error">
                {{ form_errors(form) }}
            </div>
        </div>
        <div class="small-12 medium-3 columns help">
            {% if help is defined %}
                {{ help }}
            {% endif %}
        </div>
    </div>
    {% endspaceless %}
{% endblock form_row %}

Anyone can see what I have overlooked, I tried

{% block _clinic_postcode_row %}

and

{% block _Clinic_postcode_row %}

Solution

As per accepted answer, the form row block needs to fully idetified with shorthand bundle name. The easiest way is to view the source code of the form and identify the text used in each input field and the form name="":

enter image description here

1 Answer 1

5

Replace

{% form_theme form 'AgriHealthAhpBundle:Form:fields.html.twig' %}

with

{% form_theme form with ['AgriHealthAhpBundle:Form:fields.html.twig', _self] %}

Since you are decorating the row inside action template, while applying separate form template you need to specify multiple templates

You also need to specify a fully qualified path name to your row block such as

{% block _agrihealth_ahpbundle_clinic_postcode_row %}
4
  • Thanks, that seems to make sense, but it doesn't seem to picked up still. Have cleared cache
    – jdog
    Dec 4, 2014 at 0:56
  • Probably you are specifying wrong path to the row. View the source of your generated form to identify the form name.
    – b.b3rn4rd
    Dec 4, 2014 at 2:51
  • @jdog, probably still worth to leave the screenshot
    – b.b3rn4rd
    Dec 4, 2014 at 2:58
  • 2
    Yes, I think it might be useful for others to identify a block name
    – b.b3rn4rd
    Dec 4, 2014 at 3:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.