Skip to content

Commit 763a44a

Browse files
committed
Merge branch '2.1'
2 parents 3f8ca17 + a057523 commit 763a44a

File tree

6 files changed

+113
-5
lines changed

6 files changed

+113
-5
lines changed

book/doctrine.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1369,7 +1369,7 @@ Some notable or interesting tasks include:
13691369

13701370
.. code-block:: bash
13711371
1372-
$ php app/console doctrine:ensure-production-settings --env=prod
1372+
$ php app/console doctrine:ensure-production-settings --env=prod
13731373
13741374
* ``doctrine:mapping:import`` - allows Doctrine to introspect an existing
13751375
database and create mapping information. For more information, see

book/forms.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1614,6 +1614,6 @@ Learn more from the Cookbook
16141614
.. _`Symfony2 Form Component`: https://github.com/symfony/Form
16151615
.. _`DateTime`: http://php.net/manual/en/class.datetime.php
16161616
.. _`Twig Bridge`: https://github.com/symfony/symfony/tree/master/src/Symfony/Bridge/Twig
1617-
.. _`form_div_layout.html.twig`: https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig
1617+
.. _`form_div_layout.html.twig`: https://github.com/symfony/symfony/blob/2.1/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig
16181618
.. _`Cross-site request forgery`: http://en.wikipedia.org/wiki/Cross-site_request_forgery
16191619
.. _`view on GitHub`: https://github.com/symfony/symfony/tree/master/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form

contributing/documentation/format.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ You can also add links to the API documentation:
149149

150150
.. code-block:: rst
151151
152+
:namespace:`Symfony\\Component\\BrowserKit`
153+
152154
:class:`Symfony\\Component\\Routing\\Matcher\\ApacheUrlMatcher`
153155
154156
:method:`Symfony\\Component\\HttpKernel\\Bundle\\Bundle::build`

cookbook/form/form_customization.rst

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -925,4 +925,28 @@ To render a help message below a field, pass in a ``help`` variable:
925925
.. tip::
926926
See :ref:`cookbook-form-theming-methods` for how to apply this customization.
927927

928-
.. _`form_div_layout.html.twig`: https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig
928+
Using Form Variables
929+
--------------------
930+
931+
Most of the functions available for rendering different parts of a form (e.g.
932+
the form widget, form label, form widget, etc) also allow you to make certain
933+
customizations directly. Look at the following example:
934+
935+
.. configuration-block::
936+
937+
.. code-block:: jinja
938+
939+
{# render a widget, but add a "foo" class to it #}
940+
{{ form_widget(form.name, { 'attr': {'class': 'foo'} }) }}
941+
942+
.. code-block:: php
943+
944+
<!-- render a widget, but add a "foo" class to it -->
945+
<?php echo $view['form']->widget($form['name'], array('attr' => array(
946+
'class' => 'foo',
947+
))) ?>
948+
949+
The array passed as the second argument contains form "variables". For
950+
more details about this concept in Twig, see :ref:`twig-reference-form-variables`.
951+
952+
.. _`form_div_layout.html.twig`: https://github.com/symfony/symfony/blob/2.1/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

reference/dic_tags.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,5 +771,5 @@ For an example, see the ``EntityInitializer`` class inside the Doctrine Bridge.
771771

772772
.. _`Twig's documentation`: http://twig.sensiolabs.org/doc/advanced.html#creating-an-extension
773773
.. _`Twig official extension repository`: http://github.com/fabpot/Twig-extensions
774-
.. _`KernelEvents`: https://github.com/symfony/symfony/blob/2.0/src/Symfony/Component/HttpKernel/KernelEvents.php
774+
.. _`KernelEvents`: https://github.com/symfony/symfony/blob/2.1/src/Symfony/Component/HttpKernel/KernelEvents.php
775775
.. _`SwiftMailer's Plugin Documentation`: http://swiftmailer.org/docs/plugins.html

reference/forms/twig_reference.rst

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ label you want to display as the second argument.
2323
{{ form_label(form.name, 'Your Name', {'label_attr': {'class': 'foo'}}) }}
2424
{{ form_label(form.name, null, {'label': 'Your name', 'label_attr': {'class': 'foo'}}) }}
2525
26+
See ":ref:`twig-reference-form-variables`" to learn about the ``variables``
27+
argument.
28+
2629
form_errors(form.name)
2730
----------------------
2831

@@ -50,6 +53,11 @@ The second argument to ``form_widget`` is an array of variables. The most
5053
common variable is ``attr``, which is an array of HTML attributes to apply
5154
to the HTML widget. In some cases, certain types also have other template-related
5255
options that can be passed. These are discussed on a type-by-type basis.
56+
The ``attributes`` are not applied recursively to child fields if you're
57+
rendering many fields at once (e.g. ``form_widget(form)``).
58+
59+
See ":ref:`twig-reference-form-variables`" to learn more about the ``variables``
60+
argument.
5361

5462
form_row(form.name, variables)
5563
------------------------------
@@ -66,6 +74,9 @@ The second argument to ``form_row`` is an array of variables. The templates
6674
provided in Symfony only allow to override the label as shown in the example
6775
above.
6876

77+
See ":ref:`twig-reference-form-variables`" to learn about the ``variables``
78+
argument.
79+
6980
form_rest(form, variables)
7081
--------------------------
7182

@@ -87,4 +98,75 @@ good idea to include this in your form tag:
8798

8899
.. code-block:: html+jinja
89100

90-
<form action="{{ path('form_submit') }}" method="post" {{ form_enctype(form) }}>
101+
<form action="{{ path('form_submit') }}" method="post" {{ form_enctype(form) }}>
102+
103+
.. _`twig-reference-form-variables`:
104+
105+
More about Form "Variables"
106+
---------------------------
107+
108+
In almost every Twig function above, the final argument is an array of "variables"
109+
that are used when rendering that one part of the form. For example, the
110+
following would render the "widget" for a field, and modify its attributes
111+
to include a special class:
112+
113+
.. code-block:: jinja
114+
115+
{# render a widget, but add a "foo" class to it #}
116+
{{ form_widget(form.name, { 'attr': {'class': 'foo'} }) }}
117+
118+
The purpose of these variables - what they do & where they come from - may
119+
not be immediately clear, but they're incredibly powerful. Whenever you
120+
render any part of a form, the block that renders it makes use of a number
121+
of variables. By default, these blocks live inside `form_div_layout.html.twig`_.
122+
123+
Look at the ``form_label`` as an example:
124+
125+
.. code-block:: jinja
126+
127+
{% block form_label %}
128+
{% if not compound %}
129+
{% set label_attr = label_attr|merge({'for': id}) %}
130+
{% endif %}
131+
{% if required %}
132+
{% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
133+
{% endif %}
134+
{% if label is empty %}
135+
{% set label = name|humanize %}
136+
{% endif %}
137+
<label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ label|trans({}, translation_domain) }}</label>
138+
{% endblock form_label %}
139+
140+
This block makes use of several variables: ``compound``, ``label_attr``, ``required``,
141+
``label``, ``name`` and ``translation_domain``.
142+
These variables are made available by the form rendering system. But more
143+
importantly, these are the variables that you can override when calling ``form_label``
144+
(since in this example, you're rendering the label).
145+
146+
The exact variables available to override depends on which part of the form
147+
you're rendering (e.g. label versus widget) and which field you're rendering
148+
(e.g. a ``choice`` widget has an extra ``expanded`` option). If you get comfortable
149+
with looking through `form_div_layout.html.twig`_, you'll always be able
150+
to see what options you have available.
151+
152+
.. tip::
153+
154+
Behind the scenes, these variables are made available to the ``FormView``
155+
object of your form when the form component calls ``buildView`` and ``buildViewBottomUp``
156+
on each "node" of your form tree. To see what "view" variables a particularly
157+
field has, find the source code for the form field (and its parent fields)
158+
and look at the above two functions.
159+
160+
.. note::
161+
162+
If you're rendering an entire form at once (or an entire embedded form),
163+
the ``variables`` argument will only be applied to the form itself and
164+
not its children. In other words, the following will **not** pass a "foo"
165+
class attribute to all of the child fields in the form:
166+
167+
.. code-block:: jinja
168+
169+
{# does **not** work - the variables are not recursive #}
170+
{{ form_widget(form, { 'attr': {'class': 'foo'} }) }}
171+
172+
.. _`form_div_layout.html.twig`: https://github.com/symfony/symfony/blob/2.1/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

0 commit comments

Comments
 (0)