Customize an individual Field symfony2.8

i have a variable rendered from from a controller to a twig file . i customized my form widget .. the variable works well outside the block but in the block i have this error

Variable "date_seance" does not exist in CalendarBundle:Calendar:add_seance_candidate.html.twig at line 55

the line 55 is the if

                            {{ date_seance }} ( here it works well)
 <div class="form-group">
   {% form_theme form _self %}
      {% block _autoecole_calendarbundle_calendar_start_widget %}
          {% set type = type|default('text') %}
          <input type="{{ type }}" {{ block('widget_attributes') }} {% if date_seance is not empty %} value="{{ date_seance }}" disabled="disabled" class="form-control" {% endif %} class="form-control" placeholder="Cliquer pour entrer la date du début..." />
      {% endblock %}


      {{ form_errors(form.start) }}
      ( here i display the widget start input text after customization )
      {{ form_widget(form.start) }}

 </div>

this the tutorial in symfony cookbook .. How to Customize an individual Field


Form themes blocks have isolated scope, which means they do not inherit of parent variables, except if explicitly specified.

Here, you might want to replace {{ form_widget(form.start) }} by {{ form_widget(form.start, {'date_seance' : date_seance}) }} to explicitly pass your variable to your widget.


I can guess that date_seance variable is created inside a scope which means a tag like {% block %} {% nav %} {% for %} this variable can't be accessed from outside like this :

{% block A %}
    {% set var = 'apple' %}
    {{ var }} {# This prints apple #}
{% endblock %}

{% block B %}
    {{ var }} {# This doesn't prints anything, since var isn't defined #}
{% endblock %}

To make a variable accessible outside of their scope you need to define it before:

{% set var = 'apple' %}  

{% block A %}
    {{ var }} {# This prints apple #}
{% endblock %}

In your case try to define date_seance outside of any scope

链接地址: http://www.djcxy.com/p/81400.html

上一篇: 使用Twig在Symfony子窗体中访问form.vars.value

下一篇: 自定义一个单独的Field symfony2.8