8000 merged branch stloyd/missingClientTransformer (PR #2421) · symfony/symfony@7ea9c5b · GitHub
[go: up one dir, main page]

Skip to content

Commit 7ea9c5b

Browse files
committed
merged branch stloyd/missingClientTransformer (PR #2421)
Commits ------- 49d2685 [Form] Add default validation to TextType field (and related) Discussion ---------- [Form] Add default transformer to TextType field (and related) Bug fix: yes&no (?) Feature addition: yes (?) BC break: no Symfony2 tests pass: yes Fixes the following tickets: #1962. --------------------------------------------------------------------------- by stloyd at 2011/12/19 03:43:37 -0800 @fabpot ping ;-) --------------------------------------------------------------------------- by fabpot at 2011/12/19 10:58:20 -0800 Is it really needed? I have a feeling that it enforces unneeded constraints, but I can be wrong of course. --------------------------------------------------------------------------- by hlecorche at 2011/12/20 02:31:03 -0800 It's needed because with TextType field, and without the ValueToStringTransformer, the user data (when sending the form) can be an array !!! For example: - if there is a TextType field - and if there is a MaxLengthValidator - and if the user data (when sending the form) is an array So the exception "Expected argument of type string, array given in src\Symfony\Component\Validator\Constraints\MaxLengthValidator.php at line 40" is thrown
2 parents 5803146 + 49d2685 commit 7ea9c5b

File tree

4 files changed

+170
-1
lines changed

4 files changed

+170
-1
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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\Component\Form\Extension\Core\DataTransformer;
13+
14+
use Symfony\Component\Form\DataTransformerInterface;
15+
use Symfony\Component\Form\Exception\UnexpectedTypeException;
16+
17+
/**
18+
* Transforms between a given value and a string.
19+
*
20+
* @author Joseph Bielawski <stloyd@gmail.com>
21+
*/
22+
class ValueToStringTransformer implements DataTransformerInterface
23+
{
24+
/**
25+
* Transforms a value into a string.
26+
*
27+
* @param mixed $value Mixed value.
28+
*
29+
* @return string String value.
30+
*
31+
* @throws UnexpectedTypeException if the given value is not a string or number
32+
*/
33+
public function transform($value)
34+
{
35+
if (null === $value) {
36+
return '';
37+
}
38+
39+
if (!is_string($value) && !is_numeric($value)) {
40+
throw new UnexpectedTypeException($value, 'string or number');
41+
}
42+
43+
return $value;
44+
}
45+
46+
/**
47+
* Transforms a value into a string.
48+
*
49+
* @param string $value String value.
50+
*
51+
* @return string String value.
52+
*
53+
* @throws UnexpectedTypeException if the given value is not a string
54+
*/
55+
public function reverseTransform($value)
56+
{
57+
if (null === $value) {
58+
return '';
59+
}
60+
61+
if (!is_string($value)) {
62+
throw new UnexpectedTypeException($value, 'string');
63+
}
64+
65+
return $value;
66+
}
67+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,21 @@
1212
namespace Symfony\Component\Form\Extension\Core\Type;
1313

1414
use Symfony\Component\Form\AbstractType;
15+
use Symfony\Component\Form\FormBuilder;
16+
use Symfony\Component\Form\Extension\Core\DataTransformer\ValueToStringTransformer;
1517

1618
class TextType extends AbstractType
1719
{
20+
/**
21+
* {@inheritdoc}
22+
*/
23+
public function buildForm(FormBuilder $builder, array $options)
24+
{
25+
$builder
26+
->appendClientTransformer(new ValueToStringTransformer())
27+
;
28+
}
29+
1830
/**
1931
* {@inheritdoc}
2032
*/
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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\DataTransformer;
13+
14+
use Symfony\Component\Form\Extension\Core\DataTransformer\ValueToStringTransformer;
15+
16+
class ValueToStringTransformerTest extends \PHPUnit_Framework_TestCase
17+
{
18+
protected $transformer;
19+
20+
protected function setUp()
21+
{
22+
$this->transformer = new ValueToStringTransformer();
23+
}
24+
25+
protected function tearDown()
26+
{
27+
$this->transformer = null;
28+
}
29+
30+
/**
31+
* @dataProvider validDataProvider
32+
*/
33+
public function testTransform($value, $transformed)
34+
{
35+
$this->assertEquals($transformed, $this->transformer->transform($value));
36+
}
37+
38+
/**
39+
* @dataProvider validDataProvider
40+
*/
41+
public function testReverseTransform($value, $transformed)
42+
{
43+
$this->assertEquals($transformed, $this->transformer->reverseTransform($transformed));
44+
}
45+
46+
public function validDataProvider()
47+
{
48+
return array(
49+
array('test', 'test'),
50+
array('', null),
51+
array(null, null),
52+
53+
array(0, '0'),
54+
array('0', '0'),
55+
array(1, '1'),
56+
array('123', '123'),
57+
array(1.23, '1.23'),
58+
);
59+
}
60+
61+
/**
62+
* @dataProvider invalidDataProvider
63+
*/
64+
public function testTransformExpectsStringOrNumber($value)
65+
{
66+
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
67+
68+
$this->transformer->transform($value);
69+
}
70+
71+
/**
72+
* @dataProvider invalidDataProvider
73+
*/
74+
public function testReverseTransformExpectsString($value)
75+
{
76+
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
77+
78+
$this->transformer->reverseTransform($value);
79+
}
80+
81+
public function invalidDataProvider()
82+
{
83+
return array(
84+
array(true),
85+
array(false),
86+
array(new \stdClass),
87+
array(array()),
88+
);
89+
}
90+
}

tests/Symfony/Tests/Component/Form/Extension/Core/Type/UrlTypeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function testSubmitAddsNoDefaultProtocolIfEmpty()
4646

4747
$form->bind('');
4848

49-
$this->assertNull($form->getData());
49+
$this->assertSame('', $form->getData());
5050
$this->assertSame('', $form->getClientData());
5151
}
5252

0 commit comments

Comments
 (0)
0