[WIP][Form] Improve transformation failure message by providing property path

This commit is contained in:
jubianchi 2014-07-05 16:13:07 +02:00 committed by Fabien Potencier
parent 8b54211471
commit d56b7be156
3 changed files with 74 additions and 12 deletions

View File

@ -1049,13 +1049,23 @@ class Form implements \IteratorAggregate, FormInterface
*
* @param mixed $value The value to transform
*
* @throws TransformationFailedException If the value cannot be transformed to "normalized" format
*
* @return mixed
*/
private function modelToNorm($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;
}
@ -1065,15 +1075,25 @@ class Form implements \IteratorAggregate, FormInterface
*
* @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)
{
try {
$transformers = $this->config->getModelTransformers();
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;
}
@ -1083,6 +1103,8 @@ class Form implements \IteratorAggregate, FormInterface
*
* @param mixed $value The value to transform
*
* @throws TransformationFailedException If the value cannot be transformed to "view" format
*
* @return mixed
*/
private function normToView($value)
@ -1096,9 +1118,17 @@ class Form implements \IteratorAggregate, FormInterface
return null === $value || is_scalar($value) ? (string) $value : $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;
}
@ -1108,6 +1138,8 @@ class Form implements \IteratorAggregate, FormInterface
*
* @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)
@ -1118,9 +1150,17 @@ class Form implements \IteratorAggregate, FormInterface
return '' === $value ? null : $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;
}

View File

@ -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
{
@ -26,7 +26,7 @@ class FixedDataTransformer implements DataTransformerInterface
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];
@ -37,7 +37,7 @@ class FixedDataTransformer implements DataTransformerInterface
$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;

View File

@ -74,6 +74,28 @@ class SimpleFormTest extends AbstractFormTest
$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()
{