Skip to content

Commit

Permalink
[WIP][Form] Improve transformation failure message by providing prope…
Browse files Browse the repository at this point in the history
…rty path
  • Loading branch information
jubianchi authored and fabpot committed Jul 8, 2014
1 parent 8b54211 commit d56b7be
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 12 deletions.
58 changes: 49 additions & 9 deletions src/Symfony/Component/Form/Form.php
Expand Up @@ -1049,12 +1049,22 @@ public function createView(FormView $parent = null)
*
* @param mixed $value The value to transform
*
* @throws TransformationFailedException If the value cannot be transformed to "normalized" format
*
* @return mixed
*/
private function modelToNorm($value)
{
foreach ($this->config->getModelTransformers() as $transformer) {
$value = $transformer->transform($value);
try {
foreach ($this->config->getModelTransformers() as $transformer) {
$value = $transformer->transform($value);
}
} catch (TransformationFailedException $exception) {
throw new TransformationFailedException(
'Unable to transform value for property path "' . $this->getPropertyPath() . '": ' . $exception->getMessage(),
$exception->getCode(),
$exception
);
}

return $value;
Expand All @@ -1065,14 +1075,24 @@ private function modelToNorm($value)
*
* @param string $value The value to reverse transform
*
* @throws TransformationFailedException If the value cannot be transformed to "model" format
*
* @return mixed
*/
private function normToModel($value)
{
$transformers = $this->config->getModelTransformers();
try {
$transformers = $this->config->getModelTransformers();

for ($i = count($transformers) - 1; $i >= 0; --$i) {
$value = $transformers[$i]->reverseTransform($value);
for ($i = count($transformers) - 1; $i >= 0; --$i) {
$value = $transformers[$i]->reverseTransform($value);
}
} catch (TransformationFailedException $exception) {
throw new TransformationFailedException(
'Unable to reverse value for property path "' . $this->getPropertyPath() . '": ' . $exception->getMessage(),
$exception->getCode(),
$exception
);
}

return $value;
Expand All @@ -1083,6 +1103,8 @@ private function normToModel($value)
*
* @param mixed $value The value to transform
*
* @throws TransformationFailedException If the value cannot be transformed to "view" format
*
* @return mixed
*/
private function normToView($value)
Expand All @@ -1096,8 +1118,16 @@ private function normToView($value)
return null === $value || is_scalar($value) ? (string) $value : $value;
}

foreach ($this->config->getViewTransformers() as $transformer) {
$value = $transformer->transform($value);
try {
foreach ($this->config->getViewTransformers() as $transformer) {
$value = $transformer->transform($value);
}
} catch (TransformationFailedException $exception) {
throw new TransformationFailedException(
'Unable to transform value for property path "' . $this->getPropertyPath() . '": ' . $exception->getMessage(),
$exception->getCode(),
$exception
);
}

return $value;
Expand All @@ -1108,6 +1138,8 @@ private function normToView($value)
*
* @param string $value The value to reverse transform
*
* @throws TransformationFailedException If the value cannot be transformed to "normalized" format
*
* @return mixed
*/
private function viewToNorm($value)
Expand All @@ -1118,8 +1150,16 @@ private function viewToNorm($value)
return '' === $value ? null : $value;
}

for ($i = count($transformers) - 1; $i >= 0; --$i) {
$value = $transformers[$i]->reverseTransform($value);
try {
for ($i = count($transformers) - 1; $i >= 0; --$i) {
$value = $transformers[$i]->reverseTransform($value);
}
} catch (TransformationFailedException $exception) {
throw new TransformationFailedException(
'Unable to reverse value for property path "' . $this->getPropertyPath() . '": ' . $exception->getMessage(),
$exception->getCode(),
$exception
);
}

return $value;
Expand Down
Expand Up @@ -12,7 +12,7 @@
namespace Symfony\Component\Form\Tests\Fixtures;

use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\RuntimeException;
use Symfony\Component\Form\Exception\TransformationFailedException;

class FixedDataTransformer implements DataTransformerInterface
{
Expand All @@ -26,7 +26,7 @@ public function __construct(array $mapping)
public function transform($value)
{
if (!array_key_exists($value, $this->mapping)) {
throw new RuntimeException(sprintf('No mapping for value "%s"', $value));
throw new TransformationFailedException(sprintf('No mapping for value "%s"', $value));
}

return $this->mapping[$value];
Expand All @@ -37,7 +37,7 @@ public function reverseTransform($value)
$result = array_search($value, $this->mapping, true);

if ($result === false) {
throw new RuntimeException(sprintf('No reverse mapping for value "%s"', $value));
throw new TransformationFailedException(sprintf('No reverse mapping for value "%s"', $value));
}

return $result;
Expand Down
22 changes: 22 additions & 0 deletions src/Symfony/Component/Form/Tests/SimpleFormTest.php
Expand Up @@ -74,6 +74,28 @@ public function testDataIsInitializedToConfiguredValue()
$this->assertSame('bar', $form->getViewData());
}

/**
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
* @expectedExceptionMessage Unable to transform value for property path "name": No mapping for value "arg"
*/
public function testDataTransformationFailure()
{
$model = new FixedDataTransformer(array(
'default' => 'foo',
));
$view = new FixedDataTransformer(array(
'foo' => 'bar',
));

$config = new FormConfigBuilder('name', null, $this->dispatcher);
$config->addViewTransformer($view);
$config->addModelTransformer($model);
$config->setData('arg');
$form = new Form($config);

$form->getData();
}

// https://github.com/symfony/symfony/commit/d4f4038f6daf7cf88ca7c7ab089473cce5ebf7d8#commitcomment-1632879
public function testDataIsInitializedFromSubmit()
{
Expand Down

0 comments on commit d56b7be

Please sign in to comment.