Skip to content

Commit f15121e

Browse files
committed
Added missing formats
1 parent 9250d46 commit f15121e

File tree

6 files changed

+139
-37
lines changed

6 files changed

+139
-37
lines changed

cookbook/form/create_custom_field_type.rst

Lines changed: 89 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -104,26 +104,45 @@ is "expanded" (i.e. radio buttons or checkboxes, instead of a select field),
104104
you want to always render it in a ``ul`` element. In your form theme template
105105
(see above link for details), create a ``gender_widget`` block to handle this:
106106

107-
.. code-block:: html+jinja
108-
109-
{# src/Acme/DemoBundle/Resources/views/Form/fields.html.twig #}
110-
{% block gender_widget %}
111-
{% spaceless %}
112-
{% if expanded %}
113-
<ul {{ block('widget_container_attributes') }}>
114-
{% for child in form %}
115-
<li>
116-
{{ form_widget(child) }}
117-
{{ form_label(child) }}
118-
</li>
119-
{% endfor %}
120-
</ul>
121-
{% else %}
122-
{# just let the choice widget render the select tag #}
123-
{{ block('choice_widget') }}
124-
{% endif %}
125-
{% endspaceless %}
126-
{% endblock %}
107+
.. configuration-block::
108+
109+
.. code-block:: html+jinja
110+
111+
{# src/Acme/DemoBundle/Resources/views/Form/fields.html.twig #}
112+
{% block gender_widget %}
113+
{% spaceless %}
114+
{% if expanded %}
115+
<ul {{ block('widget_container_attributes') }}>
116+
{% for child in form %}
117+
<li>
118+
{{ form_widget(child) }}
119+
{{ form_label(child) }}
120+
</li>
121+
{% endfor %}
122+
</ul>
123+
{% else %}
124+
{# just let the choice widget render the select tag #}
125+
{{ block('choice_widget') }}
126+
{% endif %}
127+
{% endspaceless %}
128+
{% endblock %}
129+
130+
.. code-block:: html+php
131+
132+
<!-- src/Acme/DemoBundle/Resources/views/Form/gender_widget.html.twig -->
133+
<?php if ($expanded) : ?>
134+
<ul <?php $view['slots']->output('widget_container_attributes') ?>>
135+
<?php foreach ($form as $child) : ?>
136+
<li>
137+
<?php echo $view['form']->widget($child) ?>
138+
<?php echo $view['form']->label($child) ?>
139+
</li>
140+
<?php endforeach ?>
141+
</ul>
142+
<?php else : ?>
143+
<!-- just let the choice widget render the select tag -->
144+
<?php echo $view['form']->renderBlock('choice_widget') ?>
145+
<?php endif ?>
127146

128147
.. note::
129148

@@ -132,13 +151,35 @@ you want to always render it in a ``ul`` element. In your form theme template
132151
Further, the main config file should point to the custom form template
133152
so that it's used when rendering all forms.
134153

135-
.. code-block:: yaml
154+
.. configuration-block::
136155

137-
# app/config/config.yml
138-
twig:
139-
form:
140-
resources:
141-
- 'AcmeDemoBundle:Form:fields.html.twig'
156+
.. code-block:: yaml
157+
158+
# app/config/config.yml
159+
twig:
160+
form:
161+
resources:
162+
- 'AcmeDemoBundle:Form:fields.html.twig'
163+
164+
.. code-block:: xml
165+
166+
<!-- app/config/config.xml -->
167+
<twig:config>
168+
<twig:form>
169+
<twig:resource>AcmeDemoBundle:Form:fields.html.twig</twig:resource>
170+
</twig:form>
171+
</twig:config>
172+
173+
.. code-block:: php
174+
175+
// app/config/config.php
176+
$container->loadFromExtension('twig', array(
177+
'form' => array(
178+
'resources' => array(
179+
'AcmeDemoBundle:Form:fields.html.twig',
180+
),
181+
),
182+
));
142183
143184
Using the Field Type
144185
--------------------
@@ -194,6 +235,12 @@ example, suppose that you're storing the gender parameters in configuration:
194235
</parameter>
195236
</parameters>
196237
238+
.. code-block:: php
239+
240+
// app/config/config.php
241+
$container->setParameter('genders.m', 'Male');
242+
$container->setParameter('genders.f', 'Female');
243+
197244
To use the parameter, define your custom field type as a service, injecting
198245
the ``genders`` parameter value as the first argument to its to-be-created
199246
``__construct`` function:
@@ -219,6 +266,21 @@ the ``genders`` parameter value as the first argument to its to-be-created
219266
<tag name="form.type" alias="gender" />
220267
</service>
221268
269+
.. code-block:: php
270+
271+
// src/Acme/DemoBundle/Resources/config/services.php
272+
use Symfony\Component\DependencyInjection\Definition;
273+
274+
$container
275+
->setDefinition('acme_demo.form.type.gender', new Definition(
276+
'Acme\DemoBundle\Form\Type\GenderType',
277+
array('%genders%')
278+
))
279+
->addTag('form.type', array(
280+
'alias' => 'gender',
281+
))
282+
;
283+
222284
.. tip::
223285

224286
Make sure the services file is being imported. See :ref:`service-container-imports-directive`
@@ -231,8 +293,8 @@ method to ``GenderType``, which receives the gender configuration::
231293

232294
// src/Acme/DemoBundle/Form/Type/GenderType.php
233295
namespace Acme\DemoBundle\Form\Type;
234-
// ...
235296

297+
// ...
236298
class GenderType extends AbstractType
237299
{
238300
private $genderChoices;

cookbook/form/create_form_type_extension.rst

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,19 @@ tag:
112112
113113
.. code-block:: xml
114114
115-
<service id="acme_demo_bundle.image_type_extension" class="Acme\DemoBundle\Form\Extension\ImageTypeExtension">
115+
<service id="acme_demo_bundle.image_type_extension"
116+
class="Acme\DemoBundle\Form\Extension\ImageTypeExtension"
117+
>
116118
<tag name="form.type_extension" alias="file" />
117119
</service>
118120
119121
.. code-block:: php
120122
121123
$container
122-
->register('acme_demo_bundle.image_type_extension', 'Acme\DemoBundle\Form\Extension\ImageTypeExtension')
124+
->register(
125+
'acme_demo_bundle.image_type_extension',
126+
'Acme\DemoBundle\Form\Extension\ImageTypeExtension'
127+
)
123128
->addTag('form.type_extension', array('alias' => 'file'));
124129
125130
The ``alias`` key of the tag is the type of field that this extension should
@@ -219,8 +224,8 @@ it in the view::
219224
/**
220225
* Store the image_path option as a builder attribute
221226
*
222-
* @param \Symfony\Component\Form\FormBuilder $builder
223-
* @param array $options
227+
* @param FormBuilder $builder
228+
* @param array $options
224229
*/
225230
public function buildForm(FormBuilder $builder, array $options)
226231
{
@@ -232,8 +237,8 @@ it in the view::
232237
/**
233238
* Pass the image url to the view
234239
*
235-
* @param \Symfony\Component\Form\FormView $view
236-
* @param \Symfony\Component\Form\FormInterface $form
240+
* @param FormView $view
241+
* @param FormInterface $form
237242
*/
238243
public function buildView(FormView $view, FormInterface $form)
239244
{
@@ -326,4 +331,4 @@ next to the file field. For example::
326331
}
327332

328333
When displaying the form, if the underlying model has already been associated
329-
with an image, you will see it displayed next to the file input.
334+
with an image, you will see it displayed next to the file input.

cookbook/form/data_transformers.rst

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ for converting to and from the issue number and the Issue object::
6565
* Transforms a string (number) to an object (issue).
6666
*
6767
* @param string $number
68+
*
6869
* @return Issue|null
70+
*
6971
* @throws TransformationFailedException if object (issue) is not found.
7072
*/
7173
public function reverseTransform($number)
@@ -287,6 +289,17 @@ it's recognized as a custom field type:
287289
<tag name="form.type" alias="issue_selector" />
288290
</service>
289291
292+
.. code-block:: php
293+
294+
$container
295+
->setDefinition('acme_demo.type.issue_selector', array(
296+
'@doctrine.orm.entity_manager'
297+
))
298+
->addTag('form.type', array(
299+
'alias' => 'issue_selector',
300+
))
301+
;
302+
290303
Now, whenever you need to use your special ``issue_selector`` field type,
291304
it's quite easy::
292305

@@ -311,4 +324,3 @@ it's quite easy::
311324
return 'task';
312325
}
313326
}
314-

cookbook/form/dynamic_form_modification.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ from this class will look the exact same regardless if a new Product is being cr
3939
or if an existing product is being edited (e.g. a product fetched from the database).
4040

4141
Suppose now, that you don't want the user to be able to change the ``name`` value
42-
once the object has been created. To do this, you can rely on Symfony's :doc:`Event Dispatcher </components/event_dispatcher/introduction>`
42+
once the object has been created. To do this, you can rely on Symfony's
43+
:doc:`Event Dispatcher </components/event_dispatcher/introduction>`
4344
system to analyze the data on the object and modify the form based on the
4445
Product object's data. In this entry, you'll learn how to add this level of
4546
flexibility to your forms.

cookbook/form/form_collections.rst

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ In your controller, you'll now initialize a new instance of ``TaskType``::
179179
if ('POST' === $request->getMethod()) {
180180
$form->bindRequest($request);
181181
if ($form->isValid()) {
182-
// maybe do some form processing, like saving the Task and Tag objects
182+
// ... maybe do some form processing, like saving the Task and Tag objects
183183
}
184184
}
185185

@@ -459,6 +459,24 @@ into new ``Tag`` objects and added to the ``tags`` property of the ``Task`` obje
459459
targetEntity: Tag
460460
cascade: [persist]
461461
462+
.. code-block:: xml
463+
464+
<!-- src/Acme/TaskBundle/Resources/config/doctrine/Task.orm.xml -->
465+
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
466+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
467+
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
468+
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
469+
470+
<entity name="Acme\TaskBundle\Entity\Task" ...>
471+
<!-- ... -->
472+
<one-to-many field="tags" target-entity="Tag">
473+
<cascade>
474+
<cascade-persists />
475+
</cascade>
476+
</one-to-many>
477+
</entity>
478+
</doctrine-mapping>
479+
462480
A second potential issue deals with the `Owning Side and Inverse Side`_
463481
of Doctrine relationships. In this example, if the "owning" side of the
464482
relationship is "Task", then persistence will work fine as the tags are

cookbook/form/form_customization.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,7 @@ and customize the ``field_errors`` fragment.
733733
<?php endif ?>
734734

735735
.. tip::
736+
736737
See :ref:`cookbook-form-theming-methods` for how to apply this customization.
737738

738739
You can also customize the error output for just one specific field type.
@@ -786,6 +787,7 @@ class to the ``div`` element around each row:
786787
</div>
787788

788789
.. tip::
790+
789791
See :ref:`cookbook-form-theming-methods` for how to apply this customization.
790792

791793
Adding a "Required" Asterisk to Field Labels
@@ -840,6 +842,7 @@ original template:
840842
<?php endif ?>
841843

842844
.. tip::
845+
843846
See :ref:`cookbook-form-theming-methods` for how to apply this customization.
844847

845848
Adding "help" messages
@@ -909,6 +912,7 @@ To render a help message below a field, pass in a ``help`` variable:
909912
<?php echo $view['form']->widget($form['title'], array('help' => 'foobar')) ?>
910913
911914
.. tip::
915+
912916
See :ref:`cookbook-form-theming-methods` for how to apply this customization.
913917

914918
Using Form Variables

0 commit comments

Comments
 (0)