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] Fixed option support in Form component #3789

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 4 commits into from
Apr 11, 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
3 changes: 2 additions & 1 deletion 3 CHANGELOG-2.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,6 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
* forms now don't create an empty object anymore if they are completely
empty and not required. The empty value for such forms is null.
* added constant Guess::VERY_HIGH_CONFIDENCE
* FormType::getDefaultOptions() now sees default options defined by parent types
* [BC BREAK] FormType::getParent() does not see default options anymore
* [BC BREAK] The methods `add`, `remove`, `setParent`, `bind` and `setData`
in class Form now throw an exception if the form is already bound
Expand All @@ -266,6 +265,8 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
"single_text" unless "with_seconds" is set to true
* checkboxes of in an expanded multiple-choice field don't include the choice
in their name anymore. Their names terminate with "[]" now.
* [BC BREAK] FormType::getDefaultOptions() and FormType::getAllowedOptionValues()
don't receive an options array anymore.

### HttpFoundation

Expand Down
38 changes: 37 additions & 1 deletion 38 UPGRADE-2.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@
```

* The options passed to the `getParent()` method of form types no longer
contain default options.
contain default options. They only contain the options passed by the user.

You should check if options exist before attempting to read their value.

Expand All @@ -303,6 +303,42 @@
return isset($options['widget']) && 'single_text' === $options['widget'] ? 'text' : 'choice';
}
```

* The methods `getDefaultOptions()` and `getAllowedOptionValues()` of form
types no longer receive an option array.

You can specify options that depend on other options using closures instead.

Before:

```
public function getDefaultOptions(array $options)
{
$defaultOptions = array();

if ($options['multiple']) {
$defaultOptions['empty_data'] = array();
}

return $defaultOptions;
}
```

After:

```
public function getDefaultOptions()
{
return array(
'empty_data' => function (Options $options, $previousValue) {
return $options['multiple'] ? array() : $previousValue;
}
);
}
```

The second argument `$previousValue` does not have to be specified if not
needed.

* The `add()`, `remove()`, `setParent()`, `bind()` and `setData()` methods in
the Form class now throw an exception if the form is already bound.
Expand Down
46 changes: 27 additions & 19 deletions 46 src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Bridge\Doctrine\Form\EventListener\MergeDoctrineCollectionListener;
use Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Options;

abstract class DoctrineType extends AbstractType
{
Expand All @@ -42,37 +43,44 @@ public function buildForm(FormBuilder $builder, array $options)
}
}

public function getDefaultOptions(array $options)
public function getDefaultOptions()
{
$defaultOptions = array(
'em' => null,
'class' => null,
'property' => null,
'query_builder' => null,
'loader' => null,
'group_by' => null,
);
$registry = $this->registry;
$type = $this;

$options = array_replace($defaultOptions, $options);
$loader = function (Options $options) use ($type, $registry) {
if (null !== $options['query_builder']) {
$manager = $registry->getManager($options['em']);

if (!isset($options['choice_list'])) {
$manager = $this->registry->getManager($options['em']);

if (isset($options['query_builder'])) {
$options['loader'] = $this->getLoader($manager, $options);
return $type->getLoader($manager, $options['query_builder'], $options['class']);
}

$defaultOptions['choice_list'] = new EntityChoiceList(
return null;
};

$choiceList = function (Options $options) use ($registry) {
$manager = $registry->getManager($options['em']);

return new EntityChoiceList(
$manager,
$options['class'],
$options['property'],
$options['loader'],
$options['choices'],
$options['group_by']
);
}
};

return $defaultOptions;
return array(
'em' => null,
'class' => null,
'property' => null,
'query_builder' => null,
'loader' => $loader,
'choices' => null,
'choice_list' => $choiceList,
'group_by' => null,
);
}

/**
Expand All @@ -82,7 +90,7 @@ public function getDefaultOptions(array $options)
* @param array $options
* @return EntityLoaderInterface
*/
abstract protected function getLoader(ObjectManager $manager, array $options);
abstract public function getLoader(ObjectManager $manager, $queryBuilder, $class);

public function getParent(array $options)
{
Expand Down
6 changes: 3 additions & 3 deletions 6 src/Symfony/Bridge/Doctrine/Form/Type/EntityType.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ class EntityType extends DoctrineType
* @param array $options
* @return ORMQueryBuilderLoader
*/
protected function getLoader(ObjectManager $manager, array $options)
public function getLoader(ObjectManager $manager, $queryBuilder, $class)
{
return new ORMQueryBuilderLoader(
$options['query_builder'],
$queryBuilder,
$manager,
$options['class']
$class
);
}

Expand Down
30 changes: 14 additions & 16 deletions 30 src/Symfony/Bridge/Propel1/Form/Type/ModelType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Bridge\Propel1\Form\ChoiceList\ModelChoiceList;
use Symfony\Bridge\Propel1\Form\DataTransformer\CollectionToArrayTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Options;
use Symfony\Component\Form\FormBuilder;

/**
Expand All @@ -30,33 +31,30 @@ public function buildForm(FormBuilder $builder, array $options)
}
}

public function getDefaultOptions(array $options)
public function getDefaultOptions()
{
$defaultOptions = array(
$choiceList = function (Options $options) {
return new ModelChoiceList(
$options['class'],
$options['property'],
$options['choices'],
$options['query'],
$options['group_by']
);
};

return array(
'template' => 'choice',
'multiple' => false,
'expanded' => false,
'class' => null,
'property' => null,
'query' => null,
'choices' => null,
'choice_list' => $choiceList,
'group_by' => null,
'by_reference' => false,
);

$options = array_replace($defaultOptions, $options);

if (!isset($options['choice_list'])) {
$defaultOptions['choice_list'] = new ModelChoiceList(
$options['class'],
$options['property'],
$options['choices'],
$options['query'],
$options['group_by']
);
}

return $defaultOptions;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why removing the return statements for the default options ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this is a typo. There should have been a return above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

public function getParent(array $options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function buildForm(FormBuilder $builder, array $options)
/**
* @see Symfony\Component\Form\AbstractType::getDefaultOptions()
*/
public function getDefaultOptions(array $options)
public function getDefaultOptions()
{
/* Note: the form's intention must correspond to that for the form login
* listener in order for the CSRF token to validate successfully.
Expand Down
4 changes: 2 additions & 2 deletions 4 src/Symfony/Component/Form/AbstractType.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function createBuilder($name, FormFactoryInterface $factory, array $optio
*
* @return array The default options
*/
public function getDefaultOptions(array $options)
public function getDefaultOptions()
{
return array();
}
Expand All @@ -108,7 +108,7 @@ public function getDefaultOptions(array $options)
*
* @return array The allowed option values
*/
public function getAllowedOptionValues(array $options)
public function getAllowedOptionValues()
{
return array();
}
Expand Down
4 changes: 2 additions & 2 deletions 4 src/Symfony/Component/Form/AbstractTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function buildViewBottomUp(FormView $view, FormInterface $form)
*
* @return array
*/
public function getDefaultOptions(array $options)
public function getDefaultOptions()
{
return array();
}
Expand All @@ -77,7 +77,7 @@ public function getDefaultOptions(array $options)
*
* @return array The allowed option values
*/
public function getAllowedOptionValues(array $options)
public function getAllowedOptionValues()
{
return array();
}
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.