[WIP][Form] Improve transformation failure message by providing property path
This commit is contained in:
parent
8b54211471
commit
d56b7be156
@ -1049,13 +1049,23 @@ class Form implements \IteratorAggregate, FormInterface
|
|||||||
*
|
*
|
||||||
* @param mixed $value The value to transform
|
* @param mixed $value The value to transform
|
||||||
*
|
*
|
||||||
|
* @throws TransformationFailedException If the value cannot be transformed to "normalized" format
|
||||||
|
*
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
private function modelToNorm($value)
|
private function modelToNorm($value)
|
||||||
{
|
{
|
||||||
|
try {
|
||||||
foreach ($this->config->getModelTransformers() as $transformer) {
|
foreach ($this->config->getModelTransformers() as $transformer) {
|
||||||
$value = $transformer->transform($value);
|
$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;
|
return $value;
|
||||||
}
|
}
|
||||||
@ -1065,15 +1075,25 @@ class Form implements \IteratorAggregate, FormInterface
|
|||||||
*
|
*
|
||||||
* @param string $value The value to reverse transform
|
* @param string $value The value to reverse transform
|
||||||
*
|
*
|
||||||
|
* @throws TransformationFailedException If the value cannot be transformed to "model" format
|
||||||
|
*
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
private function normToModel($value)
|
private function normToModel($value)
|
||||||
{
|
{
|
||||||
|
try {
|
||||||
$transformers = $this->config->getModelTransformers();
|
$transformers = $this->config->getModelTransformers();
|
||||||
|
|
||||||
for ($i = count($transformers) - 1; $i >= 0; --$i) {
|
for ($i = count($transformers) - 1; $i >= 0; --$i) {
|
||||||
$value = $transformers[$i]->reverseTransform($value);
|
$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;
|
return $value;
|
||||||
}
|
}
|
||||||
@ -1083,6 +1103,8 @@ class Form implements \IteratorAggregate, FormInterface
|
|||||||
*
|
*
|
||||||
* @param mixed $value The value to transform
|
* @param mixed $value The value to transform
|
||||||
*
|
*
|
||||||
|
* @throws TransformationFailedException If the value cannot be transformed to "view" format
|
||||||
|
*
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
private function normToView($value)
|
private function normToView($value)
|
||||||
@ -1096,9 +1118,17 @@ class Form implements \IteratorAggregate, FormInterface
|
|||||||
return null === $value || is_scalar($value) ? (string) $value : $value;
|
return null === $value || is_scalar($value) ? (string) $value : $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
foreach ($this->config->getViewTransformers() as $transformer) {
|
foreach ($this->config->getViewTransformers() as $transformer) {
|
||||||
$value = $transformer->transform($value);
|
$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;
|
return $value;
|
||||||
}
|
}
|
||||||
@ -1108,6 +1138,8 @@ class Form implements \IteratorAggregate, FormInterface
|
|||||||
*
|
*
|
||||||
* @param string $value The value to reverse transform
|
* @param string $value The value to reverse transform
|
||||||
*
|
*
|
||||||
|
* @throws TransformationFailedException If the value cannot be transformed to "normalized" format
|
||||||
|
*
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
private function viewToNorm($value)
|
private function viewToNorm($value)
|
||||||
@ -1118,9 +1150,17 @@ class Form implements \IteratorAggregate, FormInterface
|
|||||||
return '' === $value ? null : $value;
|
return '' === $value ? null : $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
for ($i = count($transformers) - 1; $i >= 0; --$i) {
|
for ($i = count($transformers) - 1; $i >= 0; --$i) {
|
||||||
$value = $transformers[$i]->reverseTransform($value);
|
$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;
|
return $value;
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
namespace Symfony\Component\Form\Tests\Fixtures;
|
namespace Symfony\Component\Form\Tests\Fixtures;
|
||||||
|
|
||||||
use Symfony\Component\Form\DataTransformerInterface;
|
use Symfony\Component\Form\DataTransformerInterface;
|
||||||
use Symfony\Component\Form\Exception\RuntimeException;
|
use Symfony\Component\Form\Exception\TransformationFailedException;
|
||||||
|
|
||||||
class FixedDataTransformer implements DataTransformerInterface
|
class FixedDataTransformer implements DataTransformerInterface
|
||||||
{
|
{
|
||||||
@ -26,7 +26,7 @@ class FixedDataTransformer implements DataTransformerInterface
|
|||||||
public function transform($value)
|
public function transform($value)
|
||||||
{
|
{
|
||||||
if (!array_key_exists($value, $this->mapping)) {
|
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];
|
return $this->mapping[$value];
|
||||||
@ -37,7 +37,7 @@ class FixedDataTransformer implements DataTransformerInterface
|
|||||||
$result = array_search($value, $this->mapping, true);
|
$result = array_search($value, $this->mapping, true);
|
||||||
|
|
||||||
if ($result === false) {
|
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;
|
return $result;
|
||||||
|
@ -74,6 +74,28 @@ class SimpleFormTest extends AbstractFormTest
|
|||||||
$this->assertSame('bar', $form->getViewData());
|
$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
|
// https://github.com/symfony/symfony/commit/d4f4038f6daf7cf88ca7c7ab089473cce5ebf7d8#commitcomment-1632879
|
||||||
public function testDataIsInitializedFromSubmit()
|
public function testDataIsInitializedFromSubmit()
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user