8000 Merge branch '2.8' into 3.0 · symfony/symfony@99be2fc · GitHub
[go: up one dir, main page]

Skip to content

Commit 99be2fc

Browse files
committed
Merge branch '2.8' into 3.0
* 2.8: [Form] refactor CheckboxListMapper and RadioListMapper Revert "[Form] refactor `RadioListMapper::mapDataToForm()`"
2 parents dd3e510 + 5c23542 commit 99be2fc

File tree

6 files changed

+105
-100
lines changed

6 files changed

+105
-100
lines changed

src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ public function testSubmitSingleExpandedNull()
274274
$field->submit(null);
275275

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

280280
public function testSubmitSingleNonExpandedNull()

src/Symfony/Bridge/Doctrine/composer.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,19 @@
2121
"symfony/polyfill-mbstring": "~1.0"
2222
},
2323
"require-dev": {
24+
<<<<<<< HEAD
2425
"symfony/stopwatch": "~2.8|~3.0",
2526
"symfony/dependency-injection": "~2.8|~3.0",
2627
"symfony/form": "~3.0",
2728
"symfony/http-kernel": "~2.8|~3.0",
2829
"symfony/property-access": "~2.8|~3.0",
30+
=======
31+
"symfony/stopwatch": "~2.2|~3.0.0",
32+
"symfony/dependency-injection": "~2.2|~3.0.0",
33+
"symfony/form": "~2.8.5|~3.0.5",
34+
"symfony/http-kernel": "~2.2|~3.0.0",
35+
"symfony/property-access": "~2.3|~3.0.0",
36+
>>>>>>> 2.8
2937
"symfony/property-info": "~2.8|3.0",
3038
"symfony/security": "~2.8|~3.0",
3139
"symfony/expression-language": "~2.8|~3.0",

src/Symfony/Component/Form/Extension/Core/DataMapper/CheckboxListMapper.php

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111

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

14-
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
1514
use Symfony\Component\Form\DataMapperInterface;
16-
use Symfony\Component\Form\Exception\TransformationFailedException;
15+
use Symfony\Component\Form\Exception\UnexpectedTypeException;
1716

1817
/**
1918
* Maps choices to/from checkbox forms.
@@ -26,16 +25,6 @@
2625
*/
2726
class CheckboxListMapper implements DataMapperInterface
2827
{
29-
/**
30-
* @var ChoiceListInterface
31-
*/
32-
private $choiceList;
33-
34-
public function __construct(ChoiceListInterface $choiceList)
35-
{
36-
$this->choiceList = $choiceList;
37-
}
38-
3928
/**
4029
* {@inheritdoc}
4130
*/
@@ -46,22 +35,12 @@ public function mapDataToForms($choices, $checkboxes)
4635
}
4736

4837
if (!is_array($choices)) {
49-
throw new TransformationFailedException('Expected an array.');
50-
}
51-
52-
try {
53-
$valueMap = array_flip($this->choiceList->getValuesForChoices($choices));
54-
} catch (\Exception $e) {
55-
throw new TransformationFailedException(
56-
'Can not read the choices from the choice list.',
57-
$e->getCode(),
58-
$e
59-
);
38+
throw new UnexpectedTypeException($choices, 'array');
6039
}
6140

6241
foreach ($checkboxes as $checkbox) {
6342
$value = $checkbox->getConfig()->getOption('value');
64-
$checkbox->setData(isset($valueMap[$value]) ? true : false);
43+
$checkbox->setData(in_array($value, $choices, true));
6544
}
6645
}
6746

@@ -70,6 +49,10 @@ public function mapDataToForms($choices, $checkboxes)
7049
*/
7150
public function mapFormsToData($checkboxes, &$choices)
7251
{
52+
if (!is_array($choices)) {
53+
throw new UnexpectedTypeException($choices, 'array');
54+
}
55+
7356
$values = array();
7457

7558
foreach ($checkboxes as $checkbox) {
@@ -79,14 +62,6 @@ public function mapFormsToData($checkboxes, &$choices)
7962
}
8063
}
8164

82-
try {
83-
$choices = $this->choiceList->getChoicesForValues($values);
84-
} catch (\Exception $e) {
85-
throw new TransformationFailedException(
86-
'Can not read the values from the choice list.',
87-
$e->getCode(),
88-
$e
89-
);
90-
}
65+
$choices = $values;
9166
}
9267
}

src/Symfony/Component/Form/Extension/Core/DataMapper/RadioListMapper.php

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

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

14-
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
1514
use Symfony\Component\Form\DataMapperInterface;
15+
use Symfony\Component\Form\Exception\UnexpectedTypeException;
1616

1717
/**
1818
* Maps choices to/from radio forms.
@@ -25,24 +25,18 @@
2525
*/
2626
class RadioListMapper implements DataMapperInterface
2727
{
28-
/**
29-
* @var ChoiceListInterface
30-
*/
31-
private $choiceList;
32-
33-
public function __construct(ChoiceListInterface $choiceList)
34-
{
35-
$this->choiceList = $choiceList;
36-
}
37-
3828
/**
3929
* {@inheritdoc}
4030
*/
41-
public function mapDataToForms($data, $radios)
31+
public function mapDataToForms($choice, $radios)
4232
{
33+
if (!is_string($choice)) {
34+
throw new UnexpectedTypeException($choice, 'string');
35+
}
36+
4337
foreach ($radios as $radio) {
4438
$value = $radio->getConfig()->getOption('value');
45-
$radio->setData($value === $data ? true : false);
39+
$radio->setData($choice === $value);
4640
}
4741
}
4842

@@ -51,6 +45,10 @@ public function mapDataToForms($data, $radios)
5145
*/
5246
public function mapFormsToData($radios, &$choice)
5347
{
48+
if (null !== $choice && !is_string($choice)) {
49+
throw new UnexpectedTypeException($choice, 'null or string');
50+
}
51+
5452
$choice = null;
5553

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

62-
$value = $radio->getConfig()->getOption('value');
63-
$choice = current($this->choiceList->getChoicesForValues(array($value)));
60+
$choice = $radio->getConfig()->getOption('value');
6461

6562
return;
6663
}

src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
6161
$builder->setAttribute('choice_list', $choiceList);
6262

6363
if ($options['expanded']) {
64-
$builder->setDataMapper($options['multiple']
65-
? new CheckboxListMapper($choiceList)
66-
: new RadioListMapper($choiceList));
64+
$builder->setDataMapper($options['multiple'] ? new CheckboxListMapper() : new RadioListMapper());
6765

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

135133
$event->setData($data);
136134
});
135+
}
137136

138-
if (!$options['multiple']) {
139-
// For radio lists, transform empty arrays to null
140-
// This is kind of a hack necessary because the RadioListMapper
141-
// is not invoked for forms without choices
142-
$builder->addEventListener(FormEvents::SUBMIT, function (FormEvent $event) {
143-
if (array() === $event->getData()) {
144-
$event->setData(null);
145-
}
146-
});
147-
// For radio lists, pre selection of the choice needs to pre set data
148-
// with the string value so it can be matched in
149-
// {@link \Symfony\Component\Form\Extension\Core\DataMapper\RadioListMapper::mapDataToForms()}
150-
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
151-
$choiceList = $event->getForm()->getConfig()->getAttribute('choice_list');
152-
$value = current($choiceList->getValuesForChoices(array($event->getData())));
153-
$event->setData((string) $value);
154-
});
155-
}
156-
} elseif ($options['multiple']) {
157-
// <select> tag with "multiple" option
137+
if ($options['multiple']) {
138+
// <select> tag with "multiple" option or list of checkbox inputs
158139
$builder->addViewTransformer(new ChoicesToValuesTransformer($choiceList));
159140
} else {
160-
// <select> tag without "multiple" option
141+
// <select> tag without "multiple" option or list of radio inputs
161142
$builder->addViewTransformer(new ChoiceToValueTransformer($choiceList));
162143
}
163144

@@ -252,7 +233,11 @@ public function finishView(FormView $view, FormInterface $form, array $options)
252233
public function configureOptions(OptionsResolver $resolver)
253234
{
254235
$emptyData = function (Options $options) {
255-
if ($options['multiple'] || $options['expanded']) {
236+
if ($options['expanded'] && !$options['multiple']) {
237+
return;
238+
}
239+
240+
if ($options['multiple']) {
256241
return array();
257242
}
258243

0 commit comments

Comments
 (0)
0