@@ -192,6 +192,24 @@ good idea to include this in your form tag:
192192
193193 <form action="{{ path('form_submit') }}" method="post" {{ form_enctype(form) }}>
194194
195+ Form Tests Reference
196+ --------------------
197+
198+ Tests can be executed by using the ``is `` operator in Twig to create a
199+ condition. Read `the Twig documentation `_ for more information.
200+
201+ .. _form-twig-selectedchoice :
202+
203+ selectedchoice(selected_value)
204+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
205+
206+ This test will check if the current choice is equal to the ``selected_value ``
207+ or if the current choice is in the array (when ``selected_value `` is an array).
208+
209+ .. code-block :: jinja
210+
211+ <option {% if choice is selectedchoice(value) %} selected="selected"{% endif %} ...>
212+
195213 .. _`twig-reference-form-variables` :
196214
197215More about Form Variables
@@ -295,47 +313,66 @@ object:
295313 <?php echo $view['form']->get('name')->vars['label'] ?>
296314 </label>
297315
298- +-----------------+-----------------------------------------------------------------------------------------+
299- | Variable | Usage |
300- +=================+=========================================================================================+
301- | ``id `` | The ``id `` HTML attribute to be rendered |
302- +-----------------+-----------------------------------------------------------------------------------------+
303- | ``name `` | The name of the field (e.g. ``title ``) - but not the ``name `` |
304- | | HTML attribute, which is ``full_name `` |
305- +-----------------+-----------------------------------------------------------------------------------------+
306- | ``full_name `` | The ``name `` HTML attribute to be rendered |
307- +-----------------+-----------------------------------------------------------------------------------------+
308- | ``errors `` | An array of any errors attached to *this * specific field (e.g. ``form.title.errors ``). |
309- | | Note that you can't use ``form.errors `` to determine if a form is valid, |
310- | | since this only returns "global" errors: some individual fields may have errors |
311- | | Instead, use the ``valid `` option |
312- +-----------------+-----------------------------------------------------------------------------------------+
313- | ``valid `` | Returns ``true `` or ``false `` depending on whether the whole form is valid |
314- +-----------------+-----------------------------------------------------------------------------------------+
315- | ``value `` | The value that will be used when rendering (commonly the ``value `` HTML attribute) |
316- +-----------------+-----------------------------------------------------------------------------------------+
317- | ``read_only `` | If ``true ``, ``readonly="readonly" `` is added to the field |
318- +-----------------+-----------------------------------------------------------------------------------------+
319- | ``disabled `` | If ``true ``, ``disabled="disabled" `` is added to the field |
320- +-----------------+-----------------------------------------------------------------------------------------+
321- | ``required `` | If ``true ``, a ``required `` attribute is added to the field to activate HTML5 |
322- | | validation. Additionally, a ``required `` class is added to the label. |
323- +-----------------+-----------------------------------------------------------------------------------------+
324- | ``max_length `` | Adds a ``maxlength `` HTML attribute to the element |
325- +-----------------+-----------------------------------------------------------------------------------------+
326- | ``pattern `` | Adds a ``pattern `` HTML attribute to the element |
327- +-----------------+-----------------------------------------------------------------------------------------+
328- | ``label `` | The string label that will be rendered |
329- +-----------------+-----------------------------------------------------------------------------------------+
330- | ``multipart `` | If ``true ``, ``form_enctype `` will render ``enctype="multipart/form-data" ``. |
331- | | This only applies to the root form element. |
332- +-----------------+-----------------------------------------------------------------------------------------+
333- | ``attr `` | A key-value array that will be rendered as HTML attributes on the field |
334- +-----------------+-----------------------------------------------------------------------------------------+
335- | ``label_attr `` | A key-value array that will be rendered as HTML attributes on the label |
336- +-----------------+-----------------------------------------------------------------------------------------+
337- | ``compound `` | Whether or not a field is actually a holder for a group of children fields |
338- | | (for example, a ``choice `` field, which is actually a group of checkboxes |
339- +-----------------+-----------------------------------------------------------------------------------------+
316+ .. versionadded :: 2.3
317+ The ``method `` and ``action `` variables were introduced in Symfony 2.3.
318+
319+ +------------------------+-------------------------------------------------------------------------------------+
320+ | Variable | Usage |
321+ +========================+=====================================================================================+
322+ | ``form `` | The current ``FormView `` instance. |
323+ +------------------------+-------------------------------------------------------------------------------------+
324+ | ``id `` | The ``id `` HTML attribute to be rendered. |
325+ +------------------------+-------------------------------------------------------------------------------------+
326+ | ``name `` | The name of the field (e.g. ``title ``) - but not the ``name `` |
327+ | | HTML attribute, which is ``full_name ``. |
328+ +------------------------+-------------------------------------------------------------------------------------+
329+ | ``full_name `` | The ``name `` HTML attribute to be rendered. |
330+ +------------------------+-------------------------------------------------------------------------------------+
331+ | ``errors `` | An array of any errors attached to *this * specific field |
332+ | | (e.g. ``form.title.errors ``). |
333+ | | Note that you can't use ``form.errors `` to determine if a form is valid, |
334+ | | since this only returns "global" errors: some individual fields may have errors. |
335+ | | Instead, use the ``valid `` option. |
336+ +------------------------+-------------------------------------------------------------------------------------+
337+ | ``valid `` | Returns ``true `` or ``false `` depending on whether the whole form is valid. |
338+ +------------------------+-------------------------------------------------------------------------------------+
339+ | ``value `` | The value that will be used when rendering (commonly the ``value `` HTML attribute). |
340+ +------------------------+-------------------------------------------------------------------------------------+
341+ | ``read_only `` | If ``true ``, ``readonly="readonly" `` is added to the field. |
342+ +------------------------+-------------------------------------------------------------------------------------+
343+ | ``disabled `` | If ``true ``, ``disabled="disabled" `` is added to the field. |
344+ +------------------------+-------------------------------------------------------------------------------------+
345+ | ``required `` | If ``true ``, a ``required `` attribute is added to the field to activate HTML5 |
346+ | | validation. Additionally, a ``required `` class is added to the label. |
347+ +------------------------+-------------------------------------------------------------------------------------+
348+ | ``max_length `` | Adds a ``maxlength `` HTML attribute to the element. |
349+ +------------------------+-------------------------------------------------------------------------------------+
350+ | ``pattern `` | Adds a ``pattern `` HTML attribute to the element. |
351+ +------------------------+-------------------------------------------------------------------------------------+
352+ | ``label `` | The string label that will be rendered. |
353+ +------------------------+-------------------------------------------------------------------------------------+
354+ | ``multipart `` | If ``true ``, ``form_enctype `` will render ``enctype="multipart/form-data" ``. |
355+ | | This only applies to the root form element. |
356+ +------------------------+-------------------------------------------------------------------------------------+
357+ | ``attr `` | A key-value array that will be rendered as HTML attributes on the field. |
358+ +------------------------+-------------------------------------------------------------------------------------+
359+ | ``label_attr `` | A key-value array that will be rendered as HTML attributes on the label. |
360+ +------------------------+-------------------------------------------------------------------------------------+
361+ | ``compound `` | Whether or not a field is actually a holder for a group of children fields |
362+ | | (for example, a ``choice `` field, which is actually a group of checkboxes. |
363+ +------------------------+-------------------------------------------------------------------------------------+
364+ | ``block_prefixes `` | An array of all the names of the parent types. |
365+ +------------------------+-------------------------------------------------------------------------------------+
366+ | ``translation_domain `` | The domain of the translations for this form. |
367+ +------------------------+-------------------------------------------------------------------------------------+
368+ | ``cache_key `` | A unique key which is used for caching. |
369+ +------------------------+-------------------------------------------------------------------------------------+
370+ | ``data `` | The normalized data of the type. |
371+ +------------------------+-------------------------------------------------------------------------------------+
372+ | ``method `` | The method of the current form (POST, GET, etc.). |
373+ +------------------------+-------------------------------------------------------------------------------------+
374+ | ``action `` | The action of the current form. |
375+ +------------------------+-------------------------------------------------------------------------------------+
340376
341377.. _`form_div_layout.html.twig` : https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig
378+ .. _`the Twig documentation` : http://twig.sensiolabs.org/doc/templates.html#test-operator
0 commit comments