Skip to content

Commit 6b04895

Browse files
committed
Merge branch '2.0' into 2.1
Conflicts: reference/forms/twig_reference.rst
2 parents 124901d + b774bc2 commit 6b04895

File tree

4 files changed

+92
-11
lines changed

4 files changed

+92
-11
lines changed

cookbook/bundles/extension.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ as integration of other related components:
2121
2222
framework:
2323
# ...
24-
form: true
24+
form: true
2525
2626
.. code-block:: xml
2727
@@ -33,7 +33,7 @@ as integration of other related components:
3333
3434
$container->loadFromExtension('framework', array(
3535
// ...
36-
'form' => true,
36+
'form' => true,
3737
// ...
3838
));
3939
@@ -548,9 +548,9 @@ When creating an extension, follow these simple conventions:
548548
* The extension should provide an XSD schema.
549549

550550
If you follow these simple conventions, your extensions will be registered
551-
automatically by Symfony2. If not, override the Bundle
552-
:method:`Symfony\\Component\\HttpKernel\\Bundle\\Bundle::build` method in
553-
your bundle::
551+
automatically by Symfony2. If not, override the
552+
:method:`Bundle::build() <Symfony\\Component\\HttpKernel\\Bundle\\Bundle::build>`
553+
method in your bundle::
554554

555555
// ...
556556
use Acme\HelloBundle\DependencyInjection\UnconventionalExtensionClass;

reference/constraints/DateTime.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Basic Usage
6464
{
6565
public static function loadValidatorMetadata(ClassMetadata $metadata)
6666
{
67-
$metadata->addPropertyConstraint('createdAt', new Assert\DataTime());
67+
$metadata->addPropertyConstraint('createdAt', new Assert\DateTime());
6868
}
6969
}
7070

reference/forms/twig_reference.rst

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
.. index::
22
single: Forms; Twig form function reference
33

4-
Twig Template Form Function Reference
5-
=====================================
4+
Twig Template Form Function and Variable Reference
5+
==================================================
6+
7+
When working with forms in a template, there are two powerful things at your
8+
disposal:
9+
10+
* :ref:`Functions<reference-form-twig-functions>` for rendering each part of a form
11+
* :ref:`Variables<twig-reference-form-variables>` for getting *any* information about any field
12+
13+
You'll use functions often to render your fields. Variables, on the other
14+
hand, are less commonly-used, but infinitely powerful since you can access
15+
a fields label, id attribute, errors, and anything else about the field.
16+
17+
.. _reference-form-twig-functions:
18+
19+
Form Rendering Functions
20+
------------------------
621

722
This reference manual covers all the possible Twig functions available for
823
rendering forms. There are several different functions available, and each
@@ -114,8 +129,12 @@ good idea to include this in your form tag:
114129

115130
.. _`twig-reference-form-variables`:
116131

117-
More about Form "Variables"
118-
---------------------------
132+
More about Form Variables
133+
-------------------------
134+
135+
.. tip::
136+
137+
For a full list of variables, see: :ref:`reference-form-twig-variables`.
119138

120139
In almost every Twig function above, the final argument is an array of "variables"
121140
that are used when rendering that one part of the form. For example, the
@@ -181,5 +200,67 @@ to see what options you have available.
181200
{# does **not** work - the variables are not recursive #}
182201
{{ form_widget(form, { 'attr': {'class': 'foo'} }) }}
183202
203+
.. _reference-form-twig-variables:
204+
205+
Form Variables Reference
206+
~~~~~~~~~~~~~~~~~~~~~~~~
207+
208+
The following variables are common to every field type. Certain field types
209+
may have even more variables and some variables here only really apply to
210+
certain types.
211+
212+
Assuming you have a ``form`` variable in your template, and you want to reference
213+
the variables on the ``name`` field, accessing the variables is done by using
214+
a public ``vars`` property on the :class:`Symfony\\Component\\Form\\FormView`
215+
object:
216+
217+
.. configuration-block::
218+
219+
.. code-block:: html+jinja
220+
221+
<label for="{{ form.name.vars.id }}"
222+
class="{{ form.name.vars.required ? 'required' : '' }}">
223+
{{ form.name.label }}
224+
</label>
225+
226+
.. code-block:: html+php
227+
228+
<label for="<?php echo $view['form']->get('name')->vars['id'] ?>"
229+
class="<?php echo $view['form']->get('name')->vars['required'] ? 'required' : '' ?>">
230+
<?php echo $view['form']->get('name')->vars['label'] ?>
231+
</label>
232+
233+
+-----------------+-----------------------------------------------------------------------------------------+
234+
| Variable | Usage |
235+
+=================+=========================================================================================+
236+
| ``id`` | The ``id`` HTML attribute to be rendered |
237+
+-----------------+-----------------------------------------------------------------------------------------+
238+
| ``name`` | The name of the field (e.g. ``title``) - but not the ``name`` |
239+
| | HTML attribute, which is ``full_name`` |
240+
+-----------------+-----------------------------------------------------------------------------------------+
241+
| ``full_name`` | The ``name`` HTML attribute to be rendered |
242+
+-----------------+-----------------------------------------------------------------------------------------+
243+
| ``errors`` | An array of any errors attached to *this* specific field (e.g. ``form.title.errors``). |
244+
| | Note that you can't use ``form.errors`` to determine if a form is valid, |
245+
| | since this only returns "global" errors: some individual fields may have errors |
246+
+-----------------+-----------------------------------------------------------------------------------------+
247+
| ``value`` | The value that will be used when rendering (commonly the ``value`` HTML attribute) |
248+
+-----------------+-----------------------------------------------------------------------------------------+
249+
| ``read_only`` | If ``true``, ``disabled="disabled"`` is added to the field |
250+
+-----------------+-----------------------------------------------------------------------------------------+
251+
| ``required`` | If ``true``, a ``required`` attribute is added to the field to activate HTML5 |
252+
| | validation. Additionally, a ``required`` class is added to the label. |
253+
+-----------------+-----------------------------------------------------------------------------------------+
254+
| ``max_length`` | Adds a ``maxlength`` HTML attribute to the element |
255+
+-----------------+-----------------------------------------------------------------------------------------+
256+
| ``pattern`` | Adds a ``pattern`` HTML attribute to the element |
257+
+-----------------+-----------------------------------------------------------------------------------------+
258+
| ``label`` | The string label that will be rendered |
259+
+-----------------+-----------------------------------------------------------------------------------------+
260+
| ``multipart`` | If ``true``, ``form_enctype`` will render ``enctype="multipart/form-data"``. |
261+
| | This only applies to the root form element. |
262+
+-----------------+-----------------------------------------------------------------------------------------+
263+
| ``attr`` | A key-value array that will be rendered as HTML attributes on the field |
264+
+-----------------+-----------------------------------------------------------------------------------------+
184265

185266
.. _`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/map.rst.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
* :doc:`Form Field Type Reference</reference/forms/types>`
2020
* :doc:`Validation Constraints Reference </reference/constraints>`
21-
* :doc:`Twig Template Function Reference</reference/forms/twig_reference>`
21+
* :doc:`Twig Template Function and Variable Reference</reference/forms/twig_reference>`
2222

2323
* :doc:`Twig Extensions (forms, filters, tags, etc) Reference</reference/twig_reference>`
2424

0 commit comments

Comments
 (0)