Skip to content

Commit

Permalink
Merge branch '3.0'
Browse files Browse the repository at this point in the history
* 3.0:
  [Form] refactor CheckboxListMapper and RadioListMapper
  Revert "[Form] refactor `RadioListMapper::mapDataToForm()`"
  • Loading branch information
fabpot committed Apr 28, 2016
2 parents 44c3e32 + 99be2fc commit 461e871
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 100 deletions.
Expand Up @@ -274,7 +274,7 @@ public function testSubmitSingleExpandedNull()
$field->submit(null);

$this->assertNull($field->getData());
$this->assertNull($field->getViewData());
$this->assertSame('', $field->getViewData(), 'View data is always a string');
}

public function testSubmitSingleNonExpandedNull()
Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Bridge/Doctrine/composer.json
Expand Up @@ -21,11 +21,19 @@
"symfony/polyfill-mbstring": "~1.0"
},
"require-dev": {
<<<<<<< HEAD
"symfony/stopwatch": "~2.8|~3.0",
"symfony/dependency-injection": "~2.8|~3.0",
"symfony/form": "~3.0",
"symfony/http-kernel": "~2.8|~3.0",
"symfony/property-access": "~2.8|~3.0",
=======
"symfony/stopwatch": "~2.2|~3.0.0",
"symfony/dependency-injection": "~2.2|~3.0.0",
"symfony/form": "~2.8.5|~3.0.5",
"symfony/http-kernel": "~2.2|~3.0.0",
"symfony/property-access": "~2.3|~3.0.0",
>>>>>>> 2.8
"symfony/property-info": "~2.8|3.0",
"symfony/security": "~2.8|~3.0",
"symfony/expression-language": "~2.8|~3.0",
Expand Down
Expand Up @@ -11,9 +11,8 @@

namespace Symfony\Component\Form\Extension\Core\DataMapper;

use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
use Symfony\Component\Form\DataMapperInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;

/**
* Maps choices to/from checkbox forms.
Expand All @@ -26,16 +25,6 @@
*/
class CheckboxListMapper implements DataMapperInterface
{
/**
* @var ChoiceListInterface
*/
private $choiceList;

public function __construct(ChoiceListInterface $choiceList)
{
$this->choiceList = $choiceList;
}

/**
* {@inheritdoc}
*/
Expand All @@ -46,22 +35,12 @@ public function mapDataToForms($choices, $checkboxes)
}

if (!is_array($choices)) {
throw new TransformationFailedException('Expected an array.');
}

try {
$valueMap = array_flip($this->choiceList->getValuesForChoices($choices));
} catch (\Exception $e) {
throw new TransformationFailedException(
'Can not read the choices from the choice list.',
$e->getCode(),
$e
);
throw new UnexpectedTypeException($choices, 'array');
}

foreach ($checkboxes as $checkbox) {
$value = $checkbox->getConfig()->getOption('value');
$checkbox->setData(isset($valueMap[$value]) ? true : false);
$checkbox->setData(in_array($value, $choices, true));
}
}

Expand All @@ -70,6 +49,10 @@ public function mapDataToForms($choices, $checkboxes)
*/
public function mapFormsToData($checkboxes, &$choices)
{
if (!is_array($choices)) {
throw new UnexpectedTypeException($choices, 'array');
}

$values = array();

foreach ($checkboxes as $checkbox) {
Expand All @@ -79,14 +62,6 @@ public function mapFormsToData($checkboxes, &$choices)
}
}

try {
$choices = $this->choiceList->getChoicesForValues($values);
} catch (\Exception $e) {
throw new TransformationFailedException(
'Can not read the values from the choice list.',
$e->getCode(),
$e
);
}
$choices = $values;
}
}
Expand Up @@ -11,8 +11,8 @@

namespace Symfony\Component\Form\Extension\Core\DataMapper;

use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
use Symfony\Component\Form\DataMapperInterface;
use Symfony\Component\Form\Exception\UnexpectedTypeException;

/**
* Maps choices to/from radio forms.
Expand All @@ -25,24 +25,18 @@
*/
class RadioListMapper implements DataMapperInterface
{
/**
* @var ChoiceListInterface
*/
private $choiceList;

public function __construct(ChoiceListInterface $choiceList)
{
$this->choiceList = $choiceList;
}

/**
* {@inheritdoc}
*/
public function mapDataToForms($data, $radios)
public function mapDataToForms($choice, $radios)
{
if (!is_string($choice)) {
throw new UnexpectedTypeException($choice, 'string');
}

foreach ($radios as $radio) {
$value = $radio->getConfig()->getOption('value');
$radio->setData($value === $data ? true : false);
$radio->setData($choice === $value);
}
}

Expand All @@ -51,6 +45,10 @@ public function mapDataToForms($data, $radios)
*/
public function mapFormsToData($radios, &$choice)
{
if (null !== $choice && !is_string($choice)) {
throw new UnexpectedTypeException($choice, 'null or string');
}

$choice = null;

foreach ($radios as $radio) {
Expand All @@ -59,8 +57,7 @@ public function mapFormsToData($radios, &$choice)
return;
}

$value = $radio->getConfig()->getOption('value');
$choice = current($this->choiceList->getChoicesForValues(array($value)));
$choice = $radio->getConfig()->getOption('value');

return;
}
Expand Down
35 changes: 10 additions & 25 deletions src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
Expand Up @@ -61,9 +61,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$builder->setAttribute('choice_list', $choiceList);

if ($options['expanded']) {
$builder->setDataMapper($options['multiple']
? new CheckboxListMapper($choiceList)
: new RadioListMapper($choiceList));
$builder->setDataMapper($options['multiple'] ? new CheckboxListMapper() : new RadioListMapper());

// Initialize all choices before doing the index check below.
// This helps in cases where index checks are optimized for non
Expand Down Expand Up @@ -134,30 +132,13 @@ public function buildForm(FormBuilderInterface $builder, array $options)

$event->setData($data);
});
}

if (!$options['multiple']) {
// For radio lists, transform empty arrays to null
// This is kind of a hack necessary because the RadioListMapper
// is not invoked for forms without choices
$builder->addEventListener(FormEvents::SUBMIT, function (FormEvent $event) {
if (array() === $event->getData()) {
$event->setData(null);
}
});
// For radio lists, pre selection of the choice needs to pre set data
// with the string value so it can be matched in
// {@link \Symfony\Component\Form\Extension\Core\DataMapper\RadioListMapper::mapDataToForms()}
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$choiceList = $event->getForm()->getConfig()->getAttribute('choice_list');
$value = current($choiceList->getValuesForChoices(array($event->getData())));
$event->setData((string) $value);
});
}
} elseif ($options['multiple']) {
// <select> tag with "multiple" option
if ($options['multiple']) {
// <select> tag with "multiple" option or list of checkbox inputs
$builder->addViewTransformer(new ChoicesToValuesTransformer($choiceList));
} else {
// <select> tag without "multiple" option
// <select> tag without "multiple" option or list of radio inputs
$builder->addViewTransformer(new ChoiceToValueTransformer($choiceList));
}

Expand Down Expand Up @@ -252,7 +233,11 @@ public function finishView(FormView $view, FormInterface $form, array $options)
public function configureOptions(OptionsResolver $resolver)
{
$emptyData = function (Options $options) {
if ($options['multiple'] || $options['expanded']) {
if ($options['expanded'] && !$options['multiple']) {
return;
}

if ($options['multiple']) {
return array();
}

Expand Down

0 comments on commit 461e871

Please sign in to comment.