Last active
October 31, 2017 22:08
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
$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