8000 Merge remote branch 'fivestar/single-choice-expanded' · renegare/symfony@91aedf5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 91aedf5

Browse files
committed
Merge remote branch 'fivestar/single-choice-expanded'
* fivestar/single-choice-expanded: [Form] Fixed FixRadioInputListener to not ignore 0. [Form] Fixed single expanded choice type to set checked attribute when passed boolean value
2 parents c6cfd3a + 790284f commit 91aedf5

File tree

4 files changed

+84
-4
lines changed

4 files changed

+84
-4
lines changed

src/Symfony/Component/Form/Extension/Core/DataTransformer/ScalarToBooleanChoicesTransformer.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,18 @@ public function transform($value)
5151
throw new UnexpectedTypeException($value, 'scalar');
5252
}
5353

54+
if (is_bool($value) || null === $value) {
55+
$value = (int)$value;
56+
}
57+
5458
try {
5559
$choices = $this->choiceList->getChoices();
5660
} catch (\Exception $e) {
5761
throw new TransformationFailedException('Can not get the choice list', $e->getCode(), $e);
5862
}
5963

6064
foreach (array_keys($choices) as $key) {
61-
$choices[$key] = $key === $value;
65+
$choices[$key] = (string)$key === (string)$value;
6266
}
6367

6468
return $choices;
@@ -91,4 +95,4 @@ public function reverseTransform($value)
9195

9296
return null;
9397
}
94-
}
98+
}

src/Symfony/Component/Form/Extension/Core/EventListener/FixRadioInputListener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ public function onBindClientData(FilterDataEvent $event)
2727
{
2828
$data = $event->getData();
2929

30-
$event->setData(empty($data) ? array() : array($data => true));
30+
$event->setData(strlen($data) < 1 ? array() : array($data => true));
3131
}
3232

3333
public static function getSubscribedEvents()
3434
{
3535
return array(FormEvents::BIND_CLIENT_DATA => 'onBindClientData');
3636
}
37-
}
37+
}

tests/Symfony/Tests/Component/Form/AbstractLayoutTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,28 @@ public function testSingleChoiceExpanded()
479479
);
480480
}
481481

482+
public function testSingleChoiceExpandedWithBooleanValue()
483+
{
484 10000 +
$form = $this->factory->createNamed('choice', 'na&me', true, array(
485+
'property_path' => 'name',
486+
'choices' => array('1' => 'Choice&A', '0' => 'Choice&B'),
487+
'multiple' => false,
488+
'expanded' => true,
489+
));
490+
491+
$this->assertWidgetMatchesXpath($form->createView(), array(),
492+
'/div
493+
[
494+
./input[@type="radio"][@name="na&me"][@id="na&me_1"][@checked]
495+
/following-sibling::label[@for="na&me_1"][.="[trans]Choice&A[/trans]"]
496+
/following-sibling::input[@type="radio"][@name="na&me"][@id="na&me_0"][not(@checked)]
497+
/following-sibling::label[@for="na&me_0"][.="[trans]Choice&B[/trans]"]
498+
]
499+
[count(./input)=2]
500+
'
501+
);
502+
}
503+
482504
public function testMultipleChoiceExpanded()
483505
{
484506
$form = $this->factory->createNamed('choice', 'na&me', array('&a', '&c'), array(
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Tests\Component\Form\Extension\Core\EventListener;
13+
14+
use Symfony\Component\Form\Event\FilterDataEvent;
15+
use Symfony\Component\Form\Extension\Core\EventListener\FixRadioInputListener;
16+
17+
class FixRadioInputListenerTest extends \PHPUnit_Framework_TestCase
18+
{
19+
public function testFixRadio()
20+
{
21+
$data = '1';
22+
$form = $this->getMock('Symfony\Tests\Component\Form\FormInterface');
23+
$event = new FilterDataEvent($form, $data);
24+
25+
$filter = new FixRadioInputListener();
26+
$filter->onBindClientData($event);
27+
28+
$this->assertEquals(array('1' => true), $event->getData());
29+
}
30+
31+
public function testFixZero()
32+
{
33+
$data = '0';
34+
$form = $this->getMock('Symfony\Tests\Component\Form\FormInterface');
35+
$event = new FilterDataEvent($form, $data);
36+
37+
$filter = new FixRadioInputListener();
38+
$filter->onBindClientData($event);
39+
40+
$this->assertEquals(array('0' => true), $event->getData());
41+
}
42+
43+
public function testIgnoreEmptyString()
44+
{
45+
$data = '';
46+
$form = $this->getMock('Symfony\Tests\Component\Form\FormInterface');
47+
$event = new FilterDataEvent($form, $data);
48+
49+
$filter = new FixRadioInputListener();
50+
$filter->onBindClientData($event);
51+
52+
$this->assertEquals(array(), $event->getData());
53+
}
54+
}

0 commit comments

Comments
 (0)
0