I used this tutorial to embed a collection of forms. How can I add validation that will ensure that the form is submitted with at least one form?
Symfony now has a Count
constraint that can be used with collection types to set a minimum number of items:
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
$formBuilder->add('example', CollectionType::class, [
// other options...
'constraints' => [
new Assert\Count([
'min' => 1,
'minMessage' => 'Must have at least one value',
// also has max and maxMessage just like the Length constraint
]),
],
]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With