Skip to content
Permalink
Browse files

[Form] Fixed option support in Form component

  • Loading branch information...
webmozart committed Apr 5, 2012
1 parent 70d49c3 commit b7330456b698f7dd10bd2171c7075fc34c13858e
Showing with 1,249 additions and 231 deletions.
  1. +2 −1 CHANGELOG-2.1.md
  2. +37 −1 UPGRADE-2.1.md
  3. +26 −19 src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php
  4. +3 −3 src/Symfony/Bridge/Doctrine/Form/Type/EntityType.php
  5. +13 −15 src/Symfony/Bridge/Propel1/Form/Type/ModelType.php
  6. +1 −1 ...fony/Bundle/SecurityBundle/Tests/Functional/Bundle/CsrfFormLoginBundle/Form/UserLoginFormType.php
  7. +2 −2 src/Symfony/Component/Form/AbstractType.php
  8. +2 −2 src/Symfony/Component/Form/AbstractTypeExtension.php
  9. +320 −0 src/Symfony/Component/Form/DefaultOptions.php
  10. +16 −0 src/Symfony/Component/Form/Exception/InvalidOptionException.php
  11. +0 −29 src/Symfony/Component/Form/Exception/InvalidOptionsException.php
  12. +0 −29 src/Symfony/Component/Form/Exception/MissingOptionsException.php
  13. +16 −0 src/Symfony/Component/Form/Exception/OptionDefinitionException.php
  14. +1 −1 src/Symfony/Component/Form/Extension/Core/Type/BirthdayType.php
  15. +1 −1 src/Symfony/Component/Form/Extension/Core/Type/CheckboxType.php
  16. +11 −5 src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
  17. +1 −1 src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php
  18. +1 −1 src/Symfony/Component/Form/Extension/Core/Type/CountryType.php
  19. +2 −2 src/Symfony/Component/Form/Extension/Core/Type/DateTimeType.php
  20. +2 −2 src/Symfony/Component/Form/Extension/Core/Type/DateType.php
  21. +31 −25 src/Symfony/Component/Form/Extension/Core/Type/FieldType.php
  22. +12 −8 src/Symfony/Component/Form/Extension/Core/Type/FormType.php
  23. +1 −1 src/Symfony/Component/Form/Extension/Core/Type/HiddenType.php
  24. +2 −2 src/Symfony/Component/Form/Extension/Core/Type/IntegerType.php
  25. +1 −1 src/Symfony/Component/Form/Extension/Core/Type/LanguageType.php
  26. +1 −1 src/Symfony/Component/Form/Extension/Core/Type/LocaleType.php
  27. +1 −1 src/Symfony/Component/Form/Extension/Core/Type/MoneyType.php
  28. +2 −2 src/Symfony/Component/Form/Extension/Core/Type/NumberType.php
  29. +1 −1 src/Symfony/Component/Form/Extension/Core/Type/PasswordType.php
  30. +2 −2 src/Symfony/Component/Form/Extension/Core/Type/PercentType.php
  31. +1 −1 src/Symfony/Component/Form/Extension/Core/Type/RepeatedType.php
  32. +2 −2 src/Symfony/Component/Form/Extension/Core/Type/TimeType.php
  33. +7 −10 src/Symfony/Component/Form/Extension/Core/Type/TimezoneType.php
  34. +1 −1 src/Symfony/Component/Form/Extension/Core/Type/UrlType.php
  35. +1 −1 src/Symfony/Component/Form/Extension/Csrf/Type/ChoiceTypeCsrfExtension.php
  36. +1 −1 src/Symfony/Component/Form/Extension/Csrf/Type/CsrfType.php
  37. +1 −1 src/Symfony/Component/Form/Extension/Csrf/Type/DateTypeCsrfExtension.php
  38. +1 −1 src/Symfony/Component/Form/Extension/Csrf/Type/FormTypeCsrfExtension.php
  39. +1 −1 src/Symfony/Component/Form/Extension/Csrf/Type/RepeatedTypeCsrfExtension.php
  40. +1 −1 src/Symfony/Component/Form/Extension/Csrf/Type/TimeTypeCsrfExtension.php
  41. +1 −1 src/Symfony/Component/Form/Extension/Validator/Type/FieldTypeValidatorExtension.php
  42. +16 −32 src/Symfony/Component/Form/FormFactory.php
  43. +2 −2 src/Symfony/Component/Form/FormTypeExtensionInterface.php
  44. +2 −2 src/Symfony/Component/Form/FormTypeInterface.php
  45. +73 −0 src/Symfony/Component/Form/LazyOption.php
  46. +364 −0 src/Symfony/Component/Form/Options.php
  47. +76 −0 src/Symfony/Component/Form/Tests/DefaultOptionsTest.php
  48. +2 −2 src/Symfony/Component/Form/Tests/Fixtures/FooType.php
  49. +1 −1 src/Symfony/Component/Form/Tests/Fixtures/FooTypeBarExtension.php
  50. +16 −12 src/Symfony/Component/Form/Tests/FormFactoryTest.php
  51. +168 −0 src/Symfony/Component/Form/Tests/OptionsTest.php
@@ -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
@@ -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

@@ -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.

@@ -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.
@@ -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
{
@@ -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,
);
}
/**
@@ -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)
{
@@ -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
);
}
@@ -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;
/**
@@ -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,
@@ -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;
}
public function getParent(array $options)
@@ -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.
@@ -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();
}
@@ -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();
}
@@ -65,7 +65,7 @@ public function buildViewBottomUp(FormView $view, FormInterface $form)
*
* @return array
*/
public function getDefaultOptions(array $options)
public function getDefaultOptions()
{
return array();
}
@@ -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();
}

0 comments on commit b733045

Please sign in to comment.
You can’t perform that action at this time.