Skip to content

Commit

Permalink
bug #31874 [Doctrine Bridge] Check field type before adding Length co…
Browse files Browse the repository at this point in the history
…nstraint (belinde)

This PR was squashed before being merged into the 4.3 branch (closes #31874).

Discussion
----------

[Doctrine Bridge] Check field type before adding Length constraint

| Q             | A
| ------------- | ---
| Branch?       | 4.3
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #31855
| License       | MIT
| Doc PR        |

Validator\DoctrineLoader now add a Length constraint only on field of type string, text and guid; for any other type the mapping length is just ignored

Commits
-------

35e6df6 [Doctrine Bridge] Check field type before adding Length constraint
  • Loading branch information
xabbuh committed Jun 6, 2019
2 parents 5777848 + 35e6df6 commit 9c8a6f9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
Expand Up @@ -60,4 +60,13 @@ class DoctrineLoaderEntity extends DoctrineLoaderParentEntity
* @ORM\Embedded(class=DoctrineLoaderEmbed::class)
*/
public $embedded;

/** @ORM\Column(type="text", nullable=true, length=1000) */
public $textField;

/** @ORM\Id @ORM\Column(type="guid", length=50) */
protected $guidField;

/** @ORM\Column(type="simple_array", length=100) */
public $simpleArrayField = [];
}
10 changes: 10 additions & 0 deletions src/Symfony/Bridge/Doctrine/Tests/Validator/DoctrineLoaderTest.php
Expand Up @@ -108,6 +108,16 @@ public function testLoadClassMetadata()
$this->assertCount(1, $embeddedMaxLengthConstraints);
$this->assertInstanceOf(Length::class, $embeddedMaxLengthConstraints[0]);
$this->assertSame(25, $embeddedMaxLengthConstraints[0]->max);

$this->assertCount(0, $classMetadata->getPropertyMetadata('guidField'));
$this->assertCount(0, $classMetadata->getPropertyMetadata('simpleArrayField'));

$textFieldMetadata = $classMetadata->getPropertyMetadata('textField');
$this->assertCount(1, $textFieldMetadata);
$textFieldConstraints = $textFieldMetadata[0]->getConstraints();
$this->assertCount(1, $textFieldConstraints);
$this->assertInstanceOf(Length::class, $textFieldConstraints[0]);
$this->assertSame(1000, $textFieldConstraints[0]->max);
}

public function testFieldMappingsConfiguration()
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Doctrine/Validator/DoctrineLoader.php
Expand Up @@ -73,7 +73,7 @@ public function loadClassMetadata(ClassMetadata $metadata): bool
$metadata->addConstraint(new UniqueEntity(['fields' => $mapping['fieldName']]));
}

if (null === ($mapping['length'] ?? null)) {
if (null === ($mapping['length'] ?? null) || !\in_array($mapping['type'], ['string', 'text'], true)) {
continue;
}

Expand Down

0 comments on commit 9c8a6f9

Please sign in to comment.