Skip to content

Commit 7dd22de

Browse files
committed
fall back to legacy options handling if configured named arguments do not match
1 parent 803603a commit 7dd22de

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

Mapping/Loader/AbstractLoader.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,15 @@ protected function newConstraint(string $name, mixed $options = null): Constrain
101101
return new $className($options);
102102
}
103103

104-
return new $className(...$options);
104+
try {
105+
return new $className(...$options);
106+
} catch (\Error $e) {
107+
if (str_starts_with($e->getMessage(), 'Unknown named parameter ')) {
108+
return new $className($options);
109+
}
110+
111+
throw $e;
112+
}
105113
}
106114

107115
if ($options) {

Tests/Mapping/Loader/XmlFileLoaderTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Validator\Constraints\Choice;
1919
use Symfony\Component\Validator\Constraints\Collection;
2020
use Symfony\Component\Validator\Constraints\IsTrue;
21+
use Symfony\Component\Validator\Constraints\Length;
2122
use Symfony\Component\Validator\Constraints\NotNull;
2223
use Symfony\Component\Validator\Constraints\Range;
2324
use Symfony\Component\Validator\Constraints\Regex;
@@ -188,4 +189,23 @@ public function testLoadConstraintWithoutNamedArgumentsSupport()
188189

189190
$loader->loadClassMetadata($metadata);
190191
}
192+
193+
/**
194+
* @group legacy
195+
*/
196+
public function testLengthConstraintValueOptionTriggersDeprecation()
197+
{
198+
$loader = new XmlFileLoader(__DIR__.'/constraint-mapping-exactly-value.xml');
199+
$metadata = new ClassMetadata(Entity_81::class);
200+
201+
$this->expectUserDeprecationMessage(\sprintf('Since symfony/validator 7.3: Passing an array of options to configure the "%s" constraint is deprecated, use named arguments instead.', Length::class));
202+
203+
$loader->loadClassMetadata($metadata);
204+
$constraints = $metadata->getPropertyMetadata('title')[0]->constraints;
205+
206+
self::assertCount(1, $constraints);
207+
self::assertInstanceOf(Length::class, $constraints[0]);
208+
self::assertSame(6, $constraints[0]->min);
209+
self::assertSame(6, $constraints[0]->max);
210+
}
191211
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping
5+
https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
6+
7+
<class name="Symfony\Component\Validator\Tests\Fixtures\Entity_81">
8+
<property name="title">
9+
<constraint name="Length">
10+
<option name="value">6</option>
11+
<option name="groups">
12+
<value>Foo</value>
13+
</option>
14+
</constraint>
15+
</property>
16+
</class>
17+
</constraint-mapping>

0 commit comments

Comments
 (0)