@@ -346,53 +346,10 @@ different than the one of your main form. Just specify both your themes:
346
346
347
347
{% form_theme form.a_child_form 'form/fields_child.html.twig' %}
348
348
349
- Form Theming in PHP
350
- -------------------
349
+ .. _referencing-base-form-blocks-twig-specific :
351
350
352
- When using PHP as a templating engine, the only method to customize a fragment
353
- is to create a new template file - this is similar to the second method used by
354
- Twig.
355
-
356
- The template file must be named after the fragment. You must create a ``integer_widget.html.php ``
357
- file in order to customize the ``integer_widget `` fragment.
358
-
359
- .. code-block :: html+php
360
-
361
- <!-- app/Resources/views/form/integer_widget.html.php -->
362
- <div class="integer_widget">
363
- <?php echo $view['form']->block(
364
- $form,
365
- 'form_widget_simple',
366
- array('type' => isset($type) ? $type : "number")
367
- ) ?>
368
- </div>
369
-
370
- Now that you've created the customized form template, you need to tell Symfony
371
- to use it. Inside the template where you're actually rendering your form,
372
- tell Symfony to use the theme via the ``setTheme() `` helper method::
373
-
374
- <?php $view['form']->setTheme($form, array(':form')); ?>
375
-
376
- <?php $view['form']->widget($form['age']) ?>
377
-
378
- When the ``form.age `` widget is rendered, Symfony will use the customized
379
- ``integer_widget.html.php `` template and the ``input `` tag will be wrapped in
380
- the ``div `` element.
381
-
382
- If you want to apply a theme to a specific child form, pass it to the ``setTheme() ``
383
- method::
384
-
385
- <?php $view['form']->setTheme($form['child'], ':form'); ?>
386
-
387
- .. note ::
388
-
389
- The ``:form `` syntax is based on the functional names for templates:
390
- ``Bundle:Directory ``. As the form directory lives in the
391
- ``app/Resources/views `` directory, the ``Bundle `` part is empty, resulting
392
- in ``:form ``.
393
-
394
- Referencing base Form Blocks (Twig specific)
395
- --------------------------------------------
351
+ Referencing base Form Blocks
352
+ ----------------------------
396
353
397
354
So far, to override a particular form block, the best method is to copy
398
355
the default block from `form_div_layout.html.twig `_, paste it into a different template,
@@ -448,16 +405,15 @@ the base block by using the ``parent()`` Twig function:
448
405
templating engine. You have to manually copy the content from the base block
449
406
to your new template file.
450
407
408
+ .. _twig :
409
+
451
410
Making Application-wide Customizations
452
411
--------------------------------------
453
412
454
413
If you'd like a certain form customization to be global to your application,
455
414
you can accomplish this by making the form customizations in an external
456
415
template and then importing it inside your application configuration.
457
416
458
- Twig
459
- ~~~~
460
-
461
417
By using the following configuration, any customized form blocks inside the
462
418
``form/fields.html.twig `` template will be used globally when a form is
463
419
rendered.
@@ -554,125 +510,6 @@ your template file rather than adding the template as a resource:
554
510
Note that the ``form `` variable in the above code is the form view variable
555
511
that you passed to your template.
556
512
557
- PHP
558
- ~~~
559
-
560
- By using the following configuration, any customized form fragments inside the
561
- ``app/Resources/views/Form `` folder will be used globally when a
562
- form is rendered.
563
-
564
- .. configuration-block ::
565
-
566
- .. code-block :: yaml
567
-
568
- # app/config/config.yml
569
- framework :
570
- templating :
571
- form :
572
- resources :
573
- - ' AppBundle:Form'
574
- # ...
575
-
576
- .. code-block :: xml
577
-
578
- <!-- app/config/config.xml -->
579
- <?xml version =" 1.0" encoding =" UTF-8" ?>
580
- <container xmlns =" http://symfony.com/schema/dic/services"
581
- xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
582
- xmlns : framework =" http://symfony.com/schema/dic/symfony"
583
- xsi : schemaLocation =" http://symfony.com/schema/dic/services
584
- http://symfony.com/schema/dic/services/services-1.0.xsd
585
- http://symfony.com/schema/dic/symfony
586
- http://symfony.com/schema/dic/symfony/symfony-1.0.xsd" >
587
-
588
- <framework : config >
589
- <framework : templating >
590
- <framework : form >
591
- <framework : resource >AppBundle:Form</framework : resource >
592
- </framework : form >
593
- </framework : templating >
594
- <!-- ... -->
595
- </framework : config >
596
- </container >
597
-
598
- .. code-block :: php
599
-
600
- // app/config/config.php
601
- // PHP
602
- $container->loadFromExtension('framework', array(
603
- 'templating' => array(
604
- 'form' => array(
605
- 'resources' => array(
606
- 'AppBundle:Form',
607
- ),
608
- ),
609
- ),
610
-
611
- // ...
612
- ));
613
-
614
- By default, the PHP engine uses a *div * layout when rendering forms. Some people,
615
- however, may prefer to render forms in a *table * layout. Use the ``FrameworkBundle:FormTable ``
616
- resource to use such a layout:
617
-
618
- .. configuration-block ::
619
-
620
- .. code-block :: yaml
621
-
622
- # app/config/config.yml
623
- framework :
624
- templating :
625
- form :
626
- resources :
627
- - ' FrameworkBundle:FormTable'
628
-
629
- .. code-block :: xml
630
-
631
- <!-- app/config/config.xml -->
632
- <?xml version =" 1.0" encoding =" UTF-8" ?>
633
- <container xmlns =" http://symfony.com/schema/dic/services"
634
- xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
635
- xmlns : framework =" http://symfony.com/schema/dic/symfony"
636
- xsi : schemaLocation =" http://symfony.com/schema/dic/services
637
- http://symfony.com/schema/dic/services/services-1.0.xsd
638
- http://symfony.com/schema/dic/symfony
639
- http://symfony.com/schema/dic/symfony/symfony-1.0.xsd" >
640
-
641
- <framework : config >
642
- <framework : templating >
643
- <framework : form >
644
- <resource >FrameworkBundle:FormTable</resource >
645
- </framework : form >
646
- </framework : templating >
647
- <!-- ... -->
648
- </framework : config >
649
- </container >
650
-
651
- .. code-block :: php
652
-
653
- // app/config/config.php
654
- $container->loadFromExtension('framework', array(
655
- 'templating' => array(
656
- 'form' => array(
657
- 'resources' => array(
658
- 'FrameworkBundle:FormTable',
659
- ),
660
- ),
661
- ),
662
-
663
- // ...
664
- ));
665
-
666
- If you only want to make the change in one template, add the following line to
667
- your template file rather than adding the template as a resource:
668
-
669
- .. code-block :: html+php
670
-
671
- <?php $view['form']->setTheme($form, array('FrameworkBundle:FormTable')); ?>
672
-
673
- Note that the ``$form `` variable in the above code is the form view variable
674
- that you passed to your template.
675
-
676
513
How to Customize an individual Field
677
514
------------------------------------
678
515
@@ -908,7 +745,7 @@ Adding a "Required" Asterisk to Field Labels
908
745
If you want to denote all of your required fields with a required asterisk (``* ``),
909
746
you can do this by customizing the ``form_label `` fragment.
910
747
911
- In Twig, if you're making the form customization inside the same template as your
748
+ If you're making the form customization inside the same template as your
912
749
form, modify the ``use `` tag and add the following:
913
750
914
751
.. code-block :: html+twig
@@ -923,7 +760,7 @@ form, modify the ``use`` tag and add the following:
923
760
{% endif %}
924
761
{% endblock %}
925
762
926
- In Twig, if you're making the form customization inside a separate template, use
763
+ If you're making the form customization inside a separate template, use
927
764
the following:
928
765
929
766
.. code-block :: html+twig
@@ -938,24 +775,6 @@ the following:
938
775
{% endif %}
939
776
{% endblock %}
940
777
941
- When using PHP as a templating engine you have to copy the content from the
942
- original template:
943
-
944
- .. code-block :: html+php
945
-
946
- <!-- form_label.html.php -->
947
-
948
- <!-- original content -->
949
- <?php if ($required) { $label_attr['class'] = trim((isset($label_attr['class']) ? $label_attr['class'] : '').' required'); } ?>
950
- <?php if (!$compound) { $label_attr['for'] = $id; } ?>
951
- <?php if (!$label) { $label = $view['form']->humanize($name); } ?>
952
- <label <?php foreach ($label_attr as $k => $v) { printf('%s="%s" ', $view->escape($k), $view->escape($v)); } ?>><?php echo $view->escape($view['translator']->trans($label, array(), $translation_domain)) ?></label>
953
-
954
- <!-- customization -->
955
- <?php if ($required) : ?>
956
- <span class="required" title="This field is required">*</span>
957
- <?php endif ?>
958
-
959
778
.. tip ::
960
779
961
780
See :ref: `form-theming-methods ` for how to apply this customization.
@@ -976,7 +795,7 @@ Adding "help" Messages
976
795
977
796
You can also customize your form widgets to have an optional "help" message.
978
797
979
- In Twig, if you're making the form customization inside the same template as your
798
+ If you're making the form customization inside the same template as your
980
799
form, modify the ``use `` tag and add the following:
981
800
982
801
.. code-block :: html+twig
@@ -991,7 +810,7 @@ form, modify the ``use`` tag and add the following:
991
810
{% endif %}
992
811
{% endblock %}
993
812
994
- In Twig, if you're making the form customization inside a separate template, use
813
+ If you're making the form customization inside a separate template, use
995
814
the following:
996
815
997
816
.. code-block :: html+twig
@@ -1006,25 +825,6 @@ the following:
1006
825
{% endif %}
1007
826
{% endblock %}
1008
827
1009
- When using PHP as a templating engine you have to copy the content from the
1010
- original template:
1011
-
1012
- .. code-block :: html+php
1013
-
1014
- <!-- form_widget_simple.html.php -->
1015
-
1016
- <!-- Original content -->
1017
- <input
1018
- type="<?php echo isset($type) ? $view->escape($type) : 'text' ?>"
1019
- <?php if (!empty($value)): ?>value="<?php echo $view->escape($value) ?>"<?php endif ?>
1020
- <?php echo $view['form']->block($form, 'widget_attributes') ?>
1021
- />
1022
-
1023
- <!-- Customization -->
1024
- <?php if (isset($help)) : ?>
1025
- <span class="help"><?php echo $view->escape($help) ?></span>
1026
- <?php endif ?>
1027
-
1028
828
To render a help message below a field, pass in a ``help `` variable:
1029
829
1030
830
.. code-block :: twig
0 commit comments