Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,15 @@ protected function newConstraint(string $name, mixed $options = null): Constrain
return new $className($options);
}

return new $className(...$options);
try {
return new $className(...$options);
} catch (\Error $e) {
if (str_starts_with($e->getMessage(), 'Unknown named parameter ')) {
return new $className($options);
}

throw $e;
}
}

if ($options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Validator\Constraints\Choice;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\IsTrue;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\Range;
use Symfony\Component\Validator\Constraints\Regex;
Expand Down Expand Up @@ -188,4 +189,23 @@ public function testLoadConstraintWithoutNamedArgumentsSupport()

$loader->loadClassMetadata($metadata);
}

/**
* @group legacy
*/
public function testLengthConstraintValueOptionTriggersDeprecation()
{
$loader = new XmlFileLoader(__DIR__.'/constraint-mapping-exactly-value.xml');
$metadata = new ClassMetadata(Entity_81::class);

$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));

$loader->loadClassMetadata($metadata);
$constraints = $metadata->getPropertyMetadata('title')[0]->constraints;

self::assertCount(1, $constraints);
self::assertInstanceOf(Length::class, $constraints[0]);
self::assertSame(6, $constraints[0]->min);
self::assertSame(6, $constraints[0]->max);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping
https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<class name="Symfony\Component\Validator\Tests\Fixtures\Entity_81">
<property name="title">
<constraint name="Length">
<option name="value">6</option>
<option name="groups">
<value>Foo</value>
</option>
</constraint>
</property>
</class>
</constraint-mapping>
Loading