Skip to content

Commit

Permalink
[Form] added the constrained method Field::isTransformationSuccessful()
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernhard Schussek authored and fabpot committed Jan 3, 2011
1 parent 8513082 commit e9a7531
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/Symfony/Component/Form/Field.php
Expand Up @@ -62,6 +62,7 @@ class Field extends Configurable implements FieldInterface
private $normalizationTransformer = null;
private $valueTransformer = null;
private $propertyPath = null;
private $transformationSuccessful = true;

public function __construct($key, array $options = array())
{
Expand Down Expand Up @@ -269,10 +270,9 @@ public function bind($taintedData)
$this->normalizedData = $this->processData($this->reverseTransform($this->transformedData));
$this->data = $this->denormalize($this->normalizedData);
$this->transformedData = $this->transform($this->normalizedData);
$this->transformationSuccessful = true;
} catch (TransformationFailedException $e) {
// TODO better text
// TESTME
$this->addError(new FieldError('invalid (localized)'));
$this->transformationSuccessful = false;
}
}

Expand Down Expand Up @@ -329,6 +329,16 @@ public function isBound()
return $this->bound;
}

/**
* Returns whether the bound value could be reverse transformed correctly
*
* @return boolean
*/
public function isTransformationSuccessful()
{
return $this->transformationSuccessful;
}

/**
* Returns whether the field is valid.
*
Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/Form/Resources/config/validation.xml
Expand Up @@ -4,6 +4,14 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.symfony-project.org/schema/dic/constraint-mapping http://www.symfony-project.org/schema/dic/services/constraint-mapping-1.0.xsd">

<class name="Symfony\Component\Form\Field">
<getter property="transformationSuccessful">
<constraint name="AssertTrue">
<option name="message">This value is invalid</option>
</constraint>
</getter>
</class>

<class name="Symfony\Component\Form\FieldGroup">
<property name="fields">
<constraint name="Valid" />
Expand Down
33 changes: 33 additions & 0 deletions tests/Symfony/Tests/Component/Form/FieldTest.php
Expand Up @@ -2,6 +2,7 @@

namespace Symfony\Tests\Component\Form;


require_once __DIR__ . '/Fixtures/Author.php';
require_once __DIR__ . '/Fixtures/TestField.php';
require_once __DIR__ . '/Fixtures/InvalidField.php';
Expand All @@ -11,6 +12,7 @@
use Symfony\Component\Form\PropertyPath;
use Symfony\Component\Form\FieldError;
use Symfony\Component\Form\FormConfiguration;
use Symfony\Component\Form\ValueTransformer\TransformationFailedException;
use Symfony\Tests\Component\Form\Fixtures\Author;
use Symfony\Tests\Component\Form\Fixtures\TestField;
use Symfony\Tests\Component\Form\Fixtures\InvalidField;
Expand Down Expand Up @@ -450,6 +452,37 @@ public function testUpdatePropertyDoesNotUpdatePropertyIfPropertyPathIsEmpty()
$this->assertEquals(null, $object->firstName);
}

public function testIsTransformationSuccessfulReturnsTrueIfReverseTransformSucceeded()
{
$field = new TestField('title', array(
'trim' => false,
));

$field->bind('a');

$this->assertEquals('a', $field->getDisplayedData());
$this->assertTrue($field->isTransformationSuccessful());
}

public function testIsTransformationSuccessfulReturnsFalseIfReverseTransformThrowsException()
{
// The value is passed to the value transformer
$transformer = $this->createMockTransformer();
$transformer->expects($this->once())
->method('reverseTransform')
->will($this->throwException(new TransformationFailedException()));

$field = new TestField('title', array(
'trim' => false,
'value_transformer' => $transformer,
));

$field->bind('a');

$this->assertEquals('a', $field->getDisplayedData());
$this->assertFalse($field->isTransformationSuccessful());
}

protected function createMockTransformer()
{
return $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface', array(), array(), '', false, false);
Expand Down

0 comments on commit e9a7531

Please sign in to comment.