Skip to content

Commit ad07284

Browse files
committed
[Form] Check the type of the constraints option
Signed-off-by: Alexander M. Turek <[email protected]>
1 parent 299b56f commit ad07284

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

Extension/Validator/Type/FormTypeValidatorExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Form\FormRendererInterface;
1919
use Symfony\Component\OptionsResolver\Options;
2020
use Symfony\Component\OptionsResolver\OptionsResolver;
21+
use Symfony\Component\Validator\Constraint;
2122
use Symfony\Component\Validator\Validator\ValidatorInterface;
2223
use Symfony\Contracts\Translation\TranslatorInterface;
2324

@@ -66,6 +67,7 @@ public function configureOptions(OptionsResolver $resolver)
6667
'allow_extra_fields' => false,
6768
'extra_fields_message' => 'This form should not contain extra fields.',
6869
]);
70+
$resolver->setAllowedTypes('constraints', [Constraint::class, Constraint::class.'[]']);
6971
$resolver->setAllowedTypes('legacy_error_messages', 'bool');
7072
$resolver->setDeprecated('legacy_error_messages', 'symfony/form', '5.2', function (Options $options, $value) {
7173
if (true === $value) {

Tests/Extension/Validator/Type/FormTypeValidatorExtensionTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Form\Tests\Extension\Core\Type\FormTypeTest;
2020
use Symfony\Component\Form\Tests\Extension\Core\Type\TextTypeTest;
2121
use Symfony\Component\Form\Tests\Fixtures\Author;
22+
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
2223
use Symfony\Component\Validator\Constraints\GroupSequence;
2324
use Symfony\Component\Validator\Constraints\Length;
2425
use Symfony\Component\Validator\Constraints\NotBlank;
@@ -61,6 +62,19 @@ public function testValidConstraint()
6162
$this->assertSame([$valid], $form->getConfig()->getOption('constraints'));
6263
}
6364

65+
public function testValidConstraintsArray()
66+
{
67+
$form = $this->createForm(['constraints' => [$valid = new Valid()]]);
68+
69+
$this->assertSame([$valid], $form->getConfig()->getOption('constraints'));
70+
}
71+
72+
public function testInvalidConstraint()
73+
{
74+
$this->expectException(InvalidOptionsException::class);
75+
$this->createForm(['constraints' => ['foo' => 'bar']]);
76+
}
77+
6478
public function testGroupSequenceWithConstraintsOption()
6579
{
6680
$form = Forms::createFormFactoryBuilder()

0 commit comments

Comments
 (0)