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
This commit is contained in:
Fabien Potencier 2011-12-21 12:55:51 +01:00
commit 7ea9c5b92a
4 changed files with 170 additions and 1 deletions

View File

@ -0,0 +1,67 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form\Extension\Core\DataTransformer;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
/**
* Transforms between a given value and a string.
*
* @author Joseph Bielawski <stloyd@gmail.com>
*/
class ValueToStringTransformer implements DataTransformerInterface
{
/**
* Transforms a value into a string.
*
* @param mixed $value Mixed value.
*
* @return string String value.
*
* @throws UnexpectedTypeException if the given value is not a string or number
*/
public function transform($value)
{
if (null === $value) {
return '';
}
if (!is_string($value) && !is_numeric($value)) {
throw new UnexpectedTypeException($value, 'string or number');
}
return $value;
}
/**
* Transforms a value into a string.
*
* @param string $value String value.
*
* @return string String value.
*
* @throws UnexpectedTypeException if the given value is not a string
*/
public function reverseTransform($value)
{
if (null === $value) {
return '';
}
if (!is_string($value)) {
throw new UnexpectedTypeException($value, 'string');
}
return $value;
}
}

View File

@ -12,9 +12,21 @@
namespace Symfony\Component\Form\Extension\Core\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\Extension\Core\DataTransformer\ValueToStringTransformer;
class TextType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->appendClientTransformer(new ValueToStringTransformer())
;
}
/**
* {@inheritdoc}
*/

View File

@ -0,0 +1,90 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Component\Form\Extension\Core\DataTransformer;
use Symfony\Component\Form\Extension\Core\DataTransformer\ValueToStringTransformer;
class ValueToStringTransformerTest extends \PHPUnit_Framework_TestCase
{
protected $transformer;
protected function setUp()
{
$this->transformer = new ValueToStringTransformer();
}
protected function tearDown()
{
$this->transformer = null;
}
/**
* @dataProvider validDataProvider
*/
public function testTransform($value, $transformed)
{
$this->assertEquals($transformed, $this->transformer->transform($value));
}
/**
* @dataProvider validDataProvider
*/
public function testReverseTransform($value, $transformed)
{
$this->assertEquals($transformed, $this->transformer->reverseTransform($transformed));
}
public function validDataProvider()
{
return array(
array('test', 'test'),
array('', null),
array(null, null),
array(0, '0'),
array('0', '0'),
array(1, '1'),
array('123', '123'),
array(1.23, '1.23'),
);
}
/**
* @dataProvider invalidDataProvider
*/
public function testTransformExpectsStringOrNumber($value)
{
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
$this->transformer->transform($value);
}
/**
* @dataProvider invalidDataProvider
*/
public function testReverseTransformExpectsString($value)
{
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
$this->transformer->reverseTransform($value);
}
public function invalidDataProvider()
{
return array(
array(true),
array(false),
array(new \stdClass),
array(array()),
);
}
}

View File

@ -46,7 +46,7 @@ class UrlTypeTest extends LocalizedTestCase
$form->bind('');
$this->assertNull($form->getData());
$this->assertSame('', $form->getData());
$this->assertSame('', $form->getClientData());
}