Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cookbook/form] Added missing formats #10

Merged
merged 1 commit into from
Feb 18, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 89 additions & 27 deletions cookbook/form/create_custom_field_type.rst
Original file line number Diff line number Diff line change
Expand Up @@ -104,26 +104,45 @@ is "expanded" (i.e. radio buttons or checkboxes, instead of a select field),
you want to always render it in a ``ul`` element. In your form theme template
(see above link for details), create a ``gender_widget`` block to handle this:

.. code-block:: html+jinja

{# src/Acme/DemoBundle/Resources/views/Form/fields.html.twig #}
{% block gender_widget %}
{% spaceless %}
{% if expanded %}
<ul {{ block('widget_container_attributes') }}>
{% for child in form %}
<li>
{{ form_widget(child) }}
{{ form_label(child) }}
</li>
{% endfor %}
</ul>
{% else %}
{# just let the choice widget render the select tag #}
{{ block('choice_widget') }}
{% endif %}
{% endspaceless %}
{% endblock %}
.. configuration-block::

.. code-block:: html+jinja

{# src/Acme/DemoBundle/Resources/views/Form/fields.html.twig #}
{% block gender_widget %}
{% spaceless %}
{% if expanded %}
<ul {{ block('widget_container_attributes') }}>
{% for child in form %}
<li>
{{ form_widget(child) }}
{{ form_label(child) }}
</li>
{% endfor %}
</ul>
{% else %}
{# just let the choice widget render the select tag #}
{{ block('choice_widget') }}
{% endif %}
{% endspaceless %}
{% endblock %}

.. code-block:: html+php

<!-- src/Acme/DemoBundle/Resources/views/Form/gender_widget.html.twig -->
<?php if ($expanded) : ?>
<ul <?php $view['slots']->output('widget_container_attributes') ?>>
<?php foreach ($form as $child) : ?>
<li>
<?php echo $view['form']->widget($child) ?>
<?php echo $view['form']->label($child) ?>
</li>
<?php endforeach ?>
</ul>
<?php else : ?>
<!-- just let the choice widget render the select tag -->
<?php echo $view['form']->renderBlock('choice_widget') ?>
<?php endif ?>

.. note::

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

.. code-block:: yaml
.. configuration-block::

# app/config/config.yml
twig:
form:
resources:
- 'AcmeDemoBundle:Form:fields.html.twig'
.. code-block:: yaml

# app/config/config.yml
twig:
form:
resources:
- 'AcmeDemoBundle:Form:fields.html.twig'

.. code-block:: xml

<!-- app/config/config.xml -->
<twig:config>
<twig:form>
<twig:resource>AcmeDemoBundle:Form:fields.html.twig</twig:resource>
</twig:form>
</twig:config>

.. code-block:: php

// app/config/config.php
$container->loadFromExtension('twig', array(
'form' => array(
'resources' => array(
'AcmeDemoBundle:Form:fields.html.twig',
),
),
));

Using the Field Type
--------------------
Expand Down Expand Up @@ -194,6 +235,12 @@ example, suppose that you're storing the gender parameters in configuration:
</parameter>
</parameters>

.. code-block:: php

// app/config/config.php
$container->setParameter('genders.m', 'Male');
$container->setParameter('genders.f', 'Female');

To use the parameter, define your custom field type as a service, injecting
the ``genders`` parameter value as the first argument to its to-be-created
``__construct`` function:
Expand All @@ -219,6 +266,21 @@ the ``genders`` parameter value as the first argument to its to-be-created
<tag name="form.type" alias="gender" />
</service>

.. code-block:: php

// src/Acme/DemoBundle/Resources/config/services.php
use Symfony\Component\DependencyInjection\Definition;

$container
->setDefinition('acme_demo.form.type.gender', new Definition(
'Acme\DemoBundle\Form\Type\GenderType',
array('%genders%')
))
->addTag('form.type', array(
'alias' => 'gender',
))
;

.. tip::

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

// src/Acme/DemoBundle/Form/Type/GenderType.php
namespace Acme\DemoBundle\Form\Type;
// ...

// ...
class GenderType extends AbstractType
{
private $genderChoices;
Expand Down
19 changes: 12 additions & 7 deletions cookbook/form/create_form_type_extension.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,19 @@ tag:

.. code-block:: xml

<service id="acme_demo_bundle.image_type_extension" class="Acme\DemoBundle\Form\Extension\ImageTypeExtension">
<service id="acme_demo_bundle.image_type_extension"
class="Acme\DemoBundle\Form\Extension\ImageTypeExtension"
>
<tag name="form.type_extension" alias="file" />
</service>

.. code-block:: php

$container
->register('acme_demo_bundle.image_type_extension', 'Acme\DemoBundle\Form\Extension\ImageTypeExtension')
->register(
'acme_demo_bundle.image_type_extension',
'Acme\DemoBundle\Form\Extension\ImageTypeExtension'
)
->addTag('form.type_extension', array('alias' => 'file'));

The ``alias`` key of the tag is the type of field that this extension should
Expand Down Expand Up @@ -219,8 +224,8 @@ it in the view::
/**
* Store the image_path option as a builder attribute
*
* @param \Symfony\Component\Form\FormBuilder $builder
* @param array $options
* @param FormBuilder $builder
* @param array $options
*/
public function buildForm(FormBuilder $builder, array $options)
{
Expand All @@ -232,8 +237,8 @@ it in the view::
/**
* Pass the image url to the view
*
* @param \Symfony\Component\Form\FormView $view
* @param \Symfony\Component\Form\FormInterface $form
* @param FormView $view
* @param FormInterface $form
*/
public function buildView(FormView $view, FormInterface $form)
{
Expand Down Expand Up @@ -326,4 +331,4 @@ next to the file field. For example::
}

When displaying the form, if the underlying model has already been associated
with an image, you will see it displayed next to the file input.
with an image, you will see it displayed next to the file input.
14 changes: 13 additions & 1 deletion cookbook/form/data_transformers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ for converting to and from the issue number and the Issue object::
* Transforms a string (number) to an object (issue).
*
* @param string $number
*
* @return Issue|null
*
* @throws TransformationFailedException if object (issue) is not found.
*/
public function reverseTransform($number)
Expand Down Expand Up @@ -287,6 +289,17 @@ it's recognized as a custom field type:
<tag name="form.type" alias="issue_selector" />
</service>

.. code-block:: php

$container
->setDefinition('acme_demo.type.issue_selector', array(
'@doctrine.orm.entity_manager'
))
->addTag('form.type', array(
'alias' => 'issue_selector',
))
;

Now, whenever you need to use your special ``issue_selector`` field type,
it's quite easy::

Expand All @@ -311,4 +324,3 @@ it's quite easy::
return 'task';
}
}

3 changes: 2 additions & 1 deletion cookbook/form/dynamic_form_modification.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ from this class will look the exact same regardless if a new Product is being cr
or if an existing product is being edited (e.g. a product fetched from the database).

Suppose now, that you don't want the user to be able to change the ``name`` value
once the object has been created. To do this, you can rely on Symfony's :doc:`Event Dispatcher </components/event_dispatcher/introduction>`
once the object has been created. To do this, you can rely on Symfony's
:doc:`Event Dispatcher </components/event_dispatcher/introduction>`
system to analyze the data on the object and modify the form based on the
Product object's data. In this entry, you'll learn how to add this level of
flexibility to your forms.
Expand Down
20 changes: 19 additions & 1 deletion cookbook/form/form_collections.rst
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ In your controller, you'll now initialize a new instance of ``TaskType``::
if ('POST' === $request->getMethod()) {
$form->bindRequest($request);
if ($form->isValid()) {
// maybe do some form processing, like saving the Task and Tag objects
// ... maybe do some form processing, like saving the Task and Tag objects
}
}

Expand Down Expand Up @@ -459,6 +459,24 @@ into new ``Tag`` objects and added to the ``tags`` property of the ``Task`` obje
targetEntity: Tag
cascade: [persist]

.. code-block:: xml

<!-- src/Acme/TaskBundle/Resources/config/doctrine/Task.orm.xml -->
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

<entity name="Acme\TaskBundle\Entity\Task" ...>
<!-- ... -->
<one-to-many field="tags" target-entity="Tag">
<cascade>
<cascade-persists />
</cascade>
</one-to-many>
</entity>
</doctrine-mapping>

A second potential issue deals with the `Owning Side and Inverse Side`_
of Doctrine relationships. In this example, if the "owning" side of the
relationship is "Task", then persistence will work fine as the tags are
Expand Down
4 changes: 4 additions & 0 deletions cookbook/form/form_customization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,7 @@ and customize the ``field_errors`` fragment.
<?php endif ?>

.. tip::

See :ref:`cookbook-form-theming-methods` for how to apply this customization.

You can also customize the error output for just one specific field type.
Expand Down Expand Up @@ -782,6 +783,7 @@ class to the ``div`` element around each row:
</div>

.. tip::

See :ref:`cookbook-form-theming-methods` for how to apply this customization.

Adding a "Required" Asterisk to Field Labels
Expand Down Expand Up @@ -836,6 +838,7 @@ original template:
<?php endif ?>

.. tip::

See :ref:`cookbook-form-theming-methods` for how to apply this customization.

Adding "help" messages
Expand Down Expand Up @@ -905,6 +908,7 @@ To render a help message below a field, pass in a ``help`` variable:
<?php echo $view['form']->widget($form['title'], array('help' => 'foobar')) ?>

.. tip::

See :ref:`cookbook-form-theming-methods` for how to apply this customization.

Using Form Variables
Expand Down