8000 [Form] Fixed option support in Form component by webmozart · Pull Request #3789 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[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 1 commit
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
Next Next commit
[Form] Fixed option support in Form component
  • Loading branch information
webmozart committed Apr 11, 2012
commit b7330456b698f7dd10bd2171c7075fc34c13858e
3 changes: 2 additions & 1 deletion 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 UPGRADE-2.1.md
Original file line number Diff line number Diff line change
6D47 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(
Copy link
Contributor

Choose a reason for hiding this comment

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

typo: return instead of $return

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, fixed.

'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
45 changes: 26 additions & 19 deletions src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php
Or F438 iginal 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,43 @@ 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,
'choice_list' => $choiceList,
'group_by' => null,
);
}

/**
Expand All @@ -82,7 +89,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 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
28 changes: 13 additions & 15 deletions 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,8 +31,18 @@ public function buildForm(FormBuilder $builder, array $options)
}
}

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

$defaultOptions = array(
'template' => 'choice',
'multiple' => false,
Expand All @@ -40,23 +51,10 @@ public function getDefaultOptions(array $options)
'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 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 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
0