Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[Form] Options are now passed to buildView() and buildViewBottomUp()
  • Loading branch information
webmozart committed May 25, 2012
1 parent 027259e commit 0ef4066
Show file tree
Hide file tree
Showing 35 changed files with 379 additions and 355 deletions.
20 changes: 20 additions & 0 deletions UPGRADE-2.1.md
Expand Up @@ -488,6 +488,8 @@
* `getErrorBubbling`
* `getNormTransformers`
* `getClientTransformers`
* `getAttribute`
* `hasAttribute`

You can access these methods on the `FormConfigInterface` object instead.

Expand Down Expand Up @@ -609,6 +611,24 @@
The second argument `$value` contains the current default value and
does not have to be specified if not needed.

* A third argument $options was added to the methods `buildView()` and
`buildViewBottomUp()` in `FormTypeInterface` and `FormTypeExtensionInterface`.
You should adapt your implementing classes.

Before:

```
public function buildView(FormView $view, FormInterface $form)
public function buildViewBottomUp(FormView $view, FormInterface $form)
```

After:

```
public function buildView(FormView $view, FormInterface $form, array $options)
public function buildViewBottomUp(FormView $view, FormInterface $form, array $options)
```

### Validator

* The methods `setMessage()`, `getMessageTemplate()` and
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Form/AbstractType.php
Expand Up @@ -35,14 +35,14 @@ public function buildForm(FormBuilder $builder, array $options)
/**
* {@inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form)
public function buildView(FormView $view, FormInterface $form, array $options)
{
}

/**
* {@inheritdoc}
*/
public function buildViewBottomUp(FormView $view, FormInterface $form)
public function buildViewBottomUp(FormView $view, FormInterface $form, array $options)
{
}

Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Form/AbstractTypeExtension.php
Expand Up @@ -28,14 +28,14 @@ public function buildForm(FormBuilder $builder, array $options)
/**
* {@inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form)
public function buildView(FormView $view, FormInterface $form, array $options)
{
}

/**
* {@inheritdoc}
*/
public function buildViewBottomUp(FormView $view, FormInterface $form)
public function buildViewBottomUp(FormView $view, FormInterface $form, array $options)
{
}

Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Expand Up @@ -81,6 +81,8 @@ CHANGELOG
* `getErrorBubbling`
* `getNormTransformers`
* `getClientTransformers`
* `getAttribute`
* `hasAttribute`
* deprecated the option "validation_constraint" in favor of the new
option "constraints"
* removed superfluous methods from DataMapperInterface
Expand All @@ -92,3 +94,9 @@ CHANGELOG
which accepts an OptionsResolver instance
* deprecated the methods `getDefaultOptions` and `getAllowedOptionValues`
in FormTypeInterface and FormTypeExtensionInterface
* options passed during construction can now be accessed from FormConfigInterface
* [BC BREAK] the options array is now passed as last argument of the
methods
* `buildView`
* `buildViewBottomUp`
in FormTypeInterface and FormTypeExtensionInterface
Expand Up @@ -27,17 +27,16 @@ public function buildForm(FormBuilder $builder, array $options)
{
$builder
->appendClientTransformer(new BooleanToStringTransformer($options['value']))
->setAttribute('value', $options['value'])
;
}

/**
* {@inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form)
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view
->set('value', $form->getAttribute('value'))
->set('value', $options['value'])
->set('checked', null !== $form->getClientData())
;
}
Expand Down
72 changes: 32 additions & 40 deletions src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
Expand Up @@ -36,41 +36,14 @@ class ChoiceType extends AbstractType
*/
public function buildForm(FormBuilder $builder, array $options)
{
if ($options['choice_list'] && !$options['choice_list'] instanceof ChoiceListInterface) {
throw new FormException('The "choice_list" must be an instance of "Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface".');
}

if (!$options['choice_list'] && !is_array($options['choices']) && !$options['choices'] instanceof \Traversable) {
throw new FormException('Either the option "choices" or "choice_list" must be set.');
}

if ($options['expanded']) {
$this->addSubForms($builder, $options['choice_list']->getPreferredViews(), $options);
$this->addSubForms($builder, $options['choice_list']->getRemainingViews(), $options);
}

// empty value
if ($options['multiple'] || $options['expanded']) {
// never use and empty value for these cases
$emptyValue = null;
} elseif (false === $options['empty_value']) {
// an empty value should be added but the user decided otherwise
$emptyValue = null;
} else {
// empty value has been set explicitly
$emptyValue = $options['empty_value'];
}

$builder
->setAttribute('choice_list', $options['choice_list'])
->setAttribute('preferred_choices', $options['preferred_choices'])
->setAttribute('multiple', $options['multiple'])
->setAttribute('expanded', $options['expanded'])
->setAttribute('required', $options['required'])
->setAttribute('empty_value', $emptyValue)
;

if ($options['expanded']) {
if ($options['multiple']) {
$builder
->appendClientTransformer(new ChoicesToBooleanArrayTransformer($options['choice_list']))
Expand Down Expand Up @@ -100,20 +73,18 @@ public function buildForm(FormBuilder $builder, array $options)
/**
* {@inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form)
public function buildView(FormView $view, FormInterface $form, array $options)
{
$choiceList = $form->getAttribute('choice_list');

$view
->set('multiple', $form->getAttribute('multiple'))
->set('expanded', $form->getAttribute('expanded'))
->set('preferred_choices', $choiceList->getPreferredViews())
->set('choices', $choiceList->getRemainingViews())
->set('multiple', $options['multiple'])
->set('expanded', $options['expanded'])
->set('preferred_choices', $options['choice_list']->getPreferredViews())
->set('choices', $options['choice_list']->getRemainingViews())
->set('separator', '-------------------')
->set('empty_value', $form->getAttribute('empty_value'))
->set('empty_value', $options['empty_value'])
;

if ($view->get('multiple') && !$view->get('expanded')) {
if ($options['multiple'] && !$options['expanded']) {
// Add "[]" to the name in case a select tag with multiple options is
// displayed. Otherwise only one of the selected options is sent in the
// POST request.
Expand All @@ -124,14 +95,14 @@ public function buildView(FormView $view, FormInterface $form)
/**
* {@inheritdoc}
*/
public function buildViewBottomUp(FormView $view, FormInterface $form)
public function buildViewBottomUp(FormView $view, FormInterface $form, array $options)
{
if ($view->get('expanded')) {
if ($options['expanded']) {
// Radio buttons should have the same name as the parent
$childName = $view->get('full_name');

// Checkboxes should append "[]" to allow multiple selection
if ($view->get('multiple')) {
if ($options['multiple']) {
$childName .= '[]';
}

Expand Down Expand Up @@ -166,6 +137,19 @@ public function setDefaultOptions(OptionsResolver $resolver)
return $options['required'] ? null : '';
};

$emptyValueFilter = function (Options $options, $emptyValue) {
if ($options['multiple'] || $options['expanded']) {
// never use an empty value for these cases
return null;
} elseif (false === $emptyValue) {
// an empty value should be added but the user decided otherwise
return null;
}

// empty value has been set explicitly
return $emptyValue;
};

$singleControl = function (Options $options) {
return !$options['expanded'];
};
Expand All @@ -179,7 +163,15 @@ public function setDefaultOptions(OptionsResolver $resolver)
'empty_data' => $emptyData,
'empty_value' => $emptyValue,
'error_bubbling' => false,
'single_control' => $singleControl,
'single_control' => $singleControl,
));

$resolver->setFilters(array(
'empty_value' => $emptyValueFilter,
));

$resolver->setAllowedTypes(array(
'choice_list' => array('null', 'Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface'),
));
}

Expand Down
20 changes: 8 additions & 12 deletions src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php
Expand Up @@ -40,34 +40,30 @@ public function buildForm(FormBuilder $builder, array $options)
$options['allow_delete']
);

$builder
->addEventSubscriber($resizeListener)
->setAttribute('allow_add', $options['allow_add'])
->setAttribute('allow_delete', $options['allow_delete'])
;
$builder->addEventSubscriber($resizeListener);
}

/**
* {@inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form)
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view
->set('allow_add', $form->getAttribute('allow_add'))
->set('allow_delete', $form->getAttribute('allow_delete'))
->set('allow_add', $options['allow_add'])
->set('allow_delete', $options['allow_delete'])
;

if ($form->hasAttribute('prototype')) {
$view->set('prototype', $form->getAttribute('prototype')->createView($view));
if ($form->getConfig()->hasAttribute('prototype')) {
$view->set('prototype', $form->getConfig()->getAttribute('prototype')->createView($view));
}
}

/**
* {@inheritdoc}
*/
public function buildViewBottomUp(FormView $view, FormInterface $form)
public function buildViewBottomUp(FormView $view, FormInterface $form, array $options)
{
if ($form->hasAttribute('prototype') && $view->get('prototype')->get('multipart')) {
if ($form->getConfig()->hasAttribute('prototype') && $view->get('prototype')->get('multipart')) {
$view->set('multipart', true);
}
}
Expand Down
Expand Up @@ -111,18 +111,16 @@ public function buildForm(FormBuilder $builder, array $options)
new DateTimeToArrayTransformer($options['data_timezone'], $options['data_timezone'], $parts)
));
}

$builder->setAttribute('widget', $options['widget']);
}

/**
* {@inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form)
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->set('widget', $form->getAttribute('widget'));
$view->set('widget', $options['widget']);

if ('single_text' === $form->getAttribute('widget')) {
if ('single_text' === $options['widget']) {
$view->set('type', 'datetime');
}
}
Expand Down
12 changes: 5 additions & 7 deletions src/Symfony/Component/Form/Extension/Core/Type/DateType.php
Expand Up @@ -130,24 +130,22 @@ public function buildForm(FormBuilder $builder, array $options)
));
}

$builder
->setAttribute('formatter', $formatter)
->setAttribute('widget', $options['widget']);
$builder->setAttribute('formatter', $formatter);
}

/**
* {@inheritdoc}
*/
public function buildViewBottomUp(FormView $view, FormInterface $form)
public function buildViewBottomUp(FormView $view, FormInterface $form, array $options)
{
$view->set('widget', $form->getAttribute('widget'));
$view->set('widget', $options['widget']);

if ('single_text' === $form->getAttribute('widget')) {
if ('single_text' === $options['widget']) {
$view->set('type', 'date');
}

if ($view->hasChildren()) {
$pattern = $form->getAttribute('formatter')->getPattern();
$pattern = $form->getConfig()->getAttribute('formatter')->getPattern();

// set right order with respect to locale (e.g.: de_DE=dd.MM.yy; en_US=M/d/yy)
// lookup various formats at http://userguide.icu-project.org/formatparse/datetime
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Form/Extension/Core/Type/FileType.php
Expand Up @@ -21,7 +21,7 @@ class FileType extends AbstractType
/**
* {@inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form)
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view
->set('type', 'file')
Expand All @@ -32,7 +32,7 @@ public function buildView(FormView $view, FormInterface $form)
/**
* {@inheritdoc}
*/
public function buildViewBottomUp(FormView $view, FormInterface $form)
public function buildViewBottomUp(FormView $view, FormInterface $form, array $options)
{
$view
->set('multipart', true)
Expand Down

0 comments on commit 0ef4066

Please sign in to comment.