Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony Twig override specific form row

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

like image 943
jdog Avatar asked Dec 03 '14 23:12

jdog


1 Answers

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 %}
like image 91
b.b3rn4rd Avatar answered Oct 14 '22 02:10

b.b3rn4rd