Skip to content

Instantly share code, notes, and snippets.

@ekiwi111
Last active October 31, 2017 22:08
Show Gist options
  • Save ekiwi111/f88d9cb33a8767c690769ce48f2177c2 to your computer and use it in GitHub Desktop.
Save ekiwi111/f88d9cb33a8767c690769ce48f2177c2 to your computer and use it in GitHub Desktop.
Symfony 2.8 - Enforce minimum select amount for the form with integer data form fields
// ...
$builder->addEventListener(FormEvents::SUBMIT, function(FormEvent $event) {
$form = $event->getForm();
$productsAmount = array_reduce($form->all(), function($carry, $item) {
$carry += (int) $item->getData();
return $carry;
}, 0);
if ($productsAmount < 1) {
$form->addError(new FormError('Please select products'));
}
});
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment