bug #39417 [Form] Fix UUID exception (jderusse)
This PR was merged into the 5.2 branch.
Discussion
----------
[Form] Fix UUID exception
| Q | A
| ------------- | ---
| Branch? | 5.2
| Bug fix? | yes
| New feature? | no
| Deprecations? | no
| Tickets | Fix #39407
| License | MIT
| Doc PR | -
When submitting an invalid UUID, the exception thrown by `Form\ChoiceList\ORMQueryBuilderLoader` should be a `TransformationFailedException` in order to be properly intercepted by the `Form\Form` class
Commits
-------
a41e7f0771
Fix exception thrown by Form when converting UUID
This commit is contained in:
commit
730b2a5d68
@ -12,8 +12,10 @@
|
||||
namespace Symfony\Bridge\Doctrine\Form\ChoiceList;
|
||||
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Doctrine\DBAL\Types\ConversionException;
|
||||
use Doctrine\DBAL\Types\Type;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Symfony\Component\Form\Exception\TransformationFailedException;
|
||||
|
||||
/**
|
||||
* Loads entities using a {@link QueryBuilder} instance.
|
||||
@ -96,7 +98,11 @@ class ORMQueryBuilderLoader implements EntityLoaderInterface
|
||||
$doctrineType = Type::getType($type);
|
||||
$platform = $qb->getEntityManager()->getConnection()->getDatabasePlatform();
|
||||
foreach ($values as &$value) {
|
||||
$value = $doctrineType->convertToDatabaseValue($value, $platform);
|
||||
try {
|
||||
$value = $doctrineType->convertToDatabaseValue($value, $platform);
|
||||
} catch (ConversionException $e) {
|
||||
throw new TransformationFailedException(sprintf('Failed to transform "%s" into "%s".', $value, $type), 0, $e);
|
||||
}
|
||||
}
|
||||
unset($value);
|
||||
}
|
||||
|
@ -20,6 +20,7 @@ use Symfony\Bridge\Doctrine\Form\ChoiceList\ORMQueryBuilderLoader;
|
||||
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
|
||||
use Symfony\Bridge\Doctrine\Types\UlidType;
|
||||
use Symfony\Bridge\Doctrine\Types\UuidType;
|
||||
use Symfony\Component\Form\Exception\TransformationFailedException;
|
||||
use Symfony\Component\Uid\Uuid;
|
||||
|
||||
class ORMQueryBuilderLoaderTest extends TestCase
|
||||
@ -188,6 +189,41 @@ class ORMQueryBuilderLoaderTest extends TestCase
|
||||
$loader->getEntitiesByIds('id', ['71c5fd46-3f16-4abb-bad7-90ac1e654a2d', '', 'b98e8e11-2897-44df-ad24-d2627eb7f499']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideUidEntityClasses
|
||||
*/
|
||||
public function testUidThrowProperException($entityClass)
|
||||
{
|
||||
if (Type::hasType('uuid')) {
|
||||
Type::overrideType('uuid', UuidType::class);
|
||||
} else {
|
||||
Type::addType('uuid', UuidType::class);
|
||||
}
|
||||
if (!Type::hasType('ulid')) {
|
||||
Type::addType('ulid', UlidType::class);
|
||||
}
|
||||
|
||||
$em = DoctrineTestHelper::createTestEntityManager();
|
||||
|
||||
$qb = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
|
||||
->setConstructorArgs([$em])
|
||||
->setMethods(['getQuery'])
|
||||
->getMock();
|
||||
|
||||
$qb->expects($this->never())
|
||||
->method('getQuery');
|
||||
|
||||
$qb->select('e')
|
||||
->from($entityClass, 'e');
|
||||
|
||||
$loader = new ORMQueryBuilderLoader($qb);
|
||||
|
||||
$this->expectException(TransformationFailedException::class);
|
||||
$this->expectExceptionMessageMatches('/^Failed to transform "hello" into "(uuid|ulid)"\.$/');
|
||||
|
||||
$loader->getEntitiesByIds('id', ['hello']);
|
||||
}
|
||||
|
||||
public function testEmbeddedIdentifierName()
|
||||
{
|
||||
if (Version::compare('2.5.0') > 0) {
|
||||
|
Reference in New Issue
Block a user