Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Form] Update the Form related documentation #1425

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

Merged
merged 1 commit into from
Jun 14, 2012
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
67 changes: 43 additions & 24 deletions 67 book/forms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -393,11 +393,13 @@ If you're creating :ref:`form classes<book-form-creating-form-classes>` (a
good practice), then you'll need to add the following to the ``getDefaultOptions()``
method::

public function getDefaultOptions()
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
return array(
$resolver->setDefaults(array(
'validation_groups' => array('registration')
);
));
}

In both of these cases, *only* the ``registration`` validation group will
Expand All @@ -414,21 +416,26 @@ If you need some advanced logic to determine the validation groups (e.g.
based on submitted data), you can set the ``validation_groups`` option
to an array callback, or a ``Closure``::

public function getDefaultOptions()
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
return array(
$resolver->setDefaults(array(
'validation_groups' => array('Acme\\AcmeBundle\\Entity\\Client', 'determineValidationGroups'),
);
));
}

This will call the static method ``determineValidationGroups()`` on the
``Client`` class after the form is bound, but before validation is executed.
The Form object is passed as an argument to that method (see next example).
You can also define whole logic inline by using a Closure::

public function getDefaultOptions()
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
return array(
$resolver->setDefaults(array(
'validation_groups' => function(FormInterface $form) {
$data = $form->getData();
if (Entity\Client::TYPE_PERSON == $data->getType()) {
Expand All @@ -437,7 +444,7 @@ You can also define whole logic inline by using a Closure::
return array('company');
}
},
);
));
}

.. index::
Expand Down Expand Up @@ -846,11 +853,13 @@ the choice is ultimately up to you.
good idea to explicitly specify the ``data_class`` option by adding the
following to your form type class::

public function getDefaultOptions()
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
return array(
$resolver->setDefaults(array(
'data_class' => 'Acme\TaskBundle\Entity\Task',
);
));
}

.. tip::
Expand All @@ -863,7 +872,9 @@ the choice is ultimately up to you.
agree with these terms" checkbox) that will not be mapped to the underlying
object, you need to set the property_path option to ``false``::

public function buildForm(FormBuilder $builder, array $options)
use Symfony\Component\Form\FormBuilderInterface;

public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('task');
$builder->add('dueDate', null, array('property_path' => false));
Expand Down Expand Up @@ -969,20 +980,21 @@ create a form class so that a ``Category`` object can be modified by the user::
namespace Acme\TaskBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class CategoryType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name');
}

public function getDefaultOptions()
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
return array(
$resolver->setDefaults(array(
'data_class' => 'Acme\TaskBundle\Entity\Category',
);
));
}

public function getName()
Expand All @@ -998,7 +1010,9 @@ class:

.. code-block:: php

public function buildForm(FormBuilder $builder, array $options)
use Symfony\Component\Form\FormBuilderInterface;

public function buildForm(FormBuilderInterface $builder, array $options)
{
// ...

Expand Down Expand Up @@ -1403,19 +1417,21 @@ that all un-rendered fields are output.

The CSRF token can be customized on a form-by-form basis. For example::

use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class TaskType extends AbstractType
{
// ...

public function getDefaultOptions()
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
return array(
$resolver->setDefaults(array(
'data_class' => 'Acme\TaskBundle\Entity\Task',
'csrf_protection' => true,
'csrf_field_name' => '_token',
// a unique key to help generate the secret token
'intention' => 'task_item',
);
));
}

// ...
Expand Down Expand Up @@ -1531,6 +1547,7 @@ method to specify the option::

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\MinLength;
use Symfony\Component\Validator\Constraints\Collection;
Expand All @@ -1539,14 +1556,16 @@ method to specify the option::
{
// ...

public function getDefaultOptions()
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$collectionConstraint = new Collection(array(
'name' => new MinLength(5),
'email' => new Email(array('message' => 'Invalid email address')),
));

return array('validation_constraint' => $collectionConstraint);
$resolver->setDefaults(array(
'validation_constraint' => $collectionConstraint
));
}
}

Expand Down
34 changes: 20 additions & 14 deletions 34 cookbook/form/create_custom_field_type.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ for form fields, which is ``<BundleName>\Form\Type``. Make sure the field extend
namespace Acme\DemoBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class GenderType extends AbstractType
{
public function getDefaultOptions()
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
return array(
$resolver->setDefaults(array(
'choices' => array(
'm' => 'Male',
'f' => 'Female',
)
);
));
}

public function getParent(array $options)
public function getParent()
{
return 'choice';
}
Expand Down Expand Up @@ -152,11 +152,11 @@ new instance of the type in one of your forms::
namespace Acme\DemoBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormBuilderInterface;

class AuthorType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('gender_code', new GenderType(), array(
'empty_value' => 'Choose a gender',
Expand Down Expand Up @@ -233,6 +233,9 @@ argument to ``GenderType``, which receives the gender configuration::

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

use Symfony\Component\OptionsResolver\OptionsResolverInterface;

// ...

class GenderType extends AbstractType
Expand All @@ -244,13 +247,13 @@ argument to ``GenderType``, which receives the gender configuration::
$this->genderChoices = $genderChoices;
}

public function getDefaultOptions()
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
return array(
'choices' => $this->genderChoices,
);
$resolver->setDefaults(array(
'data_class' => $this->genderChoices
));
}

// ...
}

Expand All @@ -260,11 +263,14 @@ configuration, using the field is now much easier::

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

use Symfony\Component\Form\FormBuilderInterface;

// ...

class AuthorType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('gender_code', 'gender', array(
'empty_value' => 'Choose a gender',
Expand All @@ -276,4 +282,4 @@ Notice that instead of instantiating a new instance, we can just refer to
it by the alias used in our service configuration, ``gender``. Have fun!

.. _`ChoiceType`: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
.. _`FieldType`: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/Type/FieldType.php
.. _`FieldType`: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/Type/FieldType.php
25 changes: 10 additions & 15 deletions 25 cookbook/form/data_transformers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ was entered::
namespace Acme\TaskBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Acme\TaskBundle\Form\DataTransformer\IssueToNumberTransformer;
use Doctrine\Common\Persistence\ObjectManager;

Expand All @@ -44,20 +45,13 @@ was entered::
$this->om = $om;
}

public function buildForm(FormBuilder $builder, array $options)
public function buildForm(FormBuilderInterface $builder, array $options)
{
$transformer = new IssueToNumberTransformer($this->om);
$builder->appendClientTransformer($transformer);
$builder->addViewTransformer($transformer);
}

public function getDefaultOptions()
{
return array(
'invalid_message' => 'The selected issue does not exist',
);
}

public function getParent(array $options)
public function getParent()
{
return 'text';
}
Expand All @@ -73,11 +67,12 @@ was entered::
You can also use transformers without creating a new custom form type
by calling ``appendClientTransformer`` on any field builder::

use Symfony\Component\Form\FormBuilderInterface;
use Acme\TaskBundle\Form\DataTransformer\IssueToNumberTransformer;

class TaskType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
public function buildForm(FormBuilderInterface $builder, array $options)
{
// ...

Expand All @@ -88,7 +83,7 @@ was entered::
// use a normal text field, but transform the text into an issue object
$builder
->add('issue', 'text')
->appendClientTransformer($transformer)
->addViewTransformer($transformer)
;
}

Expand Down Expand Up @@ -194,11 +189,11 @@ You can now add the type to your form by its alias as follows::
namespace Acme\TaskBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormBuilderInterface;

class TaskType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('task')
Expand Down
8 changes: 4 additions & 4 deletions 8 cookbook/form/dynamic_form_generation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ of what a bare form class looks like::
namespace Acme\DemoBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormBuilderInterface;

class ProductType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name');
$builder->add('price');
Expand Down Expand Up @@ -57,12 +57,12 @@ to an Event Subscriber::
namespace Acme\DemoBundle\Form

use Symfony\Component\Form\AbstractType
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormBuilderInterface;
use Acme\DemoBundle\Form\EventListener\AddNameFieldSubscriber;

class ProductType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
public function buildForm(FormBuilderInterface $builder, array $options)
{
$subscriber = new AddNameFieldSubscriber($builder->getFormFactory());
$builder->addEventSubscriber($subscriber);
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.