Skip to content

[Form] wrap multiple constraints in a Collection constraint to validate array-like data #20547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 9, 2025
Merged
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
17 changes: 8 additions & 9 deletions form/without_class.rst
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ This can be done by setting the ``constraints`` option in the
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;

Expand All @@ -149,17 +150,15 @@ This can be done by setting the ``constraints`` option in the

public function configureOptions(OptionsResolver $resolver): void
{
$constraints = [
'firstName' => new Length(['min' => 3]),
'lastName' => [
new NotBlank(),
new Length(['min' => 3]),
],
];

$resolver->setDefaults([
'data_class' => null,
'constraints' => $constraints,
'constraints' => new Collection([
'firstName' => new Length(['min' => 3]),
'lastName' => [
new NotBlank(),
new Length(['min' => 3]),
],
]),
]);
}

Expand Down
Loading