Skip to content

Commit 3c28360

Browse files
vicbweaverryan
authored andcommitted
Document the ability to use multiple themes
1 parent 087caec commit 3c28360

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

book/forms.rst

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ corresponding errors printed out with the form.
356356
a ``required`` attribute on fields that are required. For browsers that
357357
support HTML5, this will result in a native browser message being displayed
358358
if the user tries to submit the form with that field blank.
359-
359+
360360
Generated forms take full advantage of this new feature by adding sensible
361361
HTML attributes that trigger the validation. The client-side validation,
362362
however, can be disabled by adding the ``novalidate`` attribute to the
@@ -447,13 +447,13 @@ the documentation for each type.
447447
that HTML5-ready browsers will apply client-side validation if the field
448448
is left blank. If you don't want this behavior, either set the ``required``
449449
option on your field to ``false`` or :ref:`disable HTML5 validation<book-forms-html5-validation-disable>`.
450-
450+
451451
Also note that setting the ``required`` option to ``true`` will **not**
452452
result in server-side validation to be applied. In other words, if a
453453
user submits a blank value for the field (either with an old browser
454454
or web service, for example), it will be accepted as a valid value unless
455455
you use Symfony's ``NotBlank`` or ``NotNull`` validation constraint.
456-
456+
457457
In other words, the ``required`` option is "nice", but true server-side
458458
validation should *always* be used.
459459

@@ -532,7 +532,7 @@ the correct values of a number of field options.
532532
option can be guessed from the validation constrains (if ``MinLength``
533533
or ``Min`` is used) or from the Doctrine metadata (via the field's length).
534534

535-
* ``max_length``: Similar to ``min_length``, the maximum length can also
535+
* ``max_length``: Similar to ``min_length``, the maximum length can also
536536
be guessed.
537537

538538
.. note::
@@ -614,11 +614,11 @@ output can be customized on many different levels.
614614
.. tip::
615615

616616
You can access the current data of your form via ``form.vars.value``:
617-
617+
618618
.. configuration-block::
619619

620620
.. code-block:: jinja
621-
621+
622622
{{ form.vars.value.task }}
623623
624624
.. code-block:: html+php
@@ -820,15 +820,15 @@ the choice is ultimately up to you.
820820
}
821821

822822
.. tip::
823-
824-
When mapping forms to objects, all fields are mapped. Any fields on the
823+
824+
When mapping forms to objects, all fields are mapped. Any fields on the
825825
form that do not exist on the mapped object will cause an exception to
826826
be thrown.
827-
827+
828828
In cases where you need extra fields in the form (for example: a "do you
829829
agree with these terms" checkbox) that will not be mapped to the underlying
830830
object, you need to set the property_path option to ``false``::
831-
831+
832832
public function buildForm(FormBuilder $builder, array $options)
833833
{
834834
$builder->add('task');
@@ -1085,6 +1085,8 @@ renders the form:
10851085

10861086
{% form_theme form 'AcmeTaskBundle:Form:fields.html.twig' %}
10871087

1088+
{% form_theme form 'AcmeTaskBundle:Form:fields.html.twig' 'AcmeTaskBundle:Form:fields2.html.twig' %}
1089+
10881090
<form ...>
10891091

10901092
.. code-block:: html+php
@@ -1093,6 +1095,8 @@ renders the form:
10931095

10941096
<?php $view['form']->setTheme($form, array('AcmeTaskBundle:Form')) ?>
10951097

1098+
<?php $view['form']->setTheme($form, array('AcmeTaskBundle:Form', 'AcmeTaskBundle:Form')) ?>
1099+
10961100
<form ...>
10971101

10981102
The ``form_theme`` tag (in Twig) "imports" the fragments defined in the given
@@ -1101,6 +1105,13 @@ template and uses them when rendering the form. In other words, when the
11011105
block from your custom theme (instead of the default ``field_row`` block
11021106
that ships with Symfony).
11031107

1108+
Your custom theme does not have to override all the blocks. When rendering a block
1109+
which is not overridden in your custom theme, the theming engine will fall back
1110+
to the global theme (defined at the bundle level).
1111+
1112+
If several custom themes are provided they will be searched in the listed order
1113+
before falling back to the global theme.
1114+
11041115
To customize any portion of a form, you just need to override the appropriate
11051116
fragment. Knowing exactly which block or file to override is the subject of
11061117
the next section.
@@ -1348,7 +1359,7 @@ The CSRF token can be customized on a form-by-form basis. For example::
13481359
class TaskType extends AbstractType
13491360
{
13501361
// ...
1351-
1362+
13521363
public function getDefaultOptions(array $options)
13531364
{
13541365
return array(
@@ -1359,7 +1370,7 @@ The CSRF token can be customized on a form-by-form basis. For example::
13591370
'intention' => 'task_item',
13601371
);
13611372
}
1362-
1373+
13631374
// ...
13641375
}
13651376

@@ -1398,14 +1409,14 @@ an array of the submitted data. This is actually really easy::
13981409
->add('email', 'email')
13991410
->add('message', 'textarea')
14001411
->getForm();
1401-
1412+
14021413
if ($request->getMethod() == 'POST') {
14031414
$form->bindRequest($request);
14041415

14051416
// data is an array with "name", "email", and "message" keys
14061417
$data = $form->getData();
14071418
}
1408-
1419+
14091420
// ... render the form
14101421
}
14111422

@@ -1425,14 +1436,14 @@ an array.
14251436

14261437
.. tip::
14271438

1428-
You can also access POST values (in this case "name") directly through
1439+
You can also access POST values (in this case "name") directly through
14291440
the request object, like so:
14301441

14311442
.. code-block:: php
14321443
14331444
$this->get('request')->request->get('name');
14341445
1435-
Be advised, however, that in most cases using the getData() method is
1446+
Be advised, however, that in most cases using the getData() method is
14361447
a better choice, since it returns the data (usually an object) after
14371448
it's been transformed by the form framework.
14381449

@@ -1487,15 +1498,15 @@ method to specify the option::
14871498
'name' => new MinLength(5),
14881499
'email' => new Email(array('message' => 'Invalid email address')),
14891500
));
1490-
1501+
14911502
return array('validation_constraint' => $collectionConstraint);
14921503
}
14931504
}
14941505

14951506
Now, you have the flexibility to create forms - with validation - that return
14961507
an array of data, instead of an object. In most cases, it's better - and
14971508
certainly more robust - to bind your form to an object. But for simple forms,
1498-
this is a great approach.
1509+
this is a great approach.
14991510

15001511
Final Thoughts
15011512
--------------

0 commit comments

Comments
 (0)