Skip to content

Instantly share code, notes, and snippets.

@micronax
Created April 27, 2015 08:50
Show Gist options
  • Save micronax/fa4ee3e2f920fcc6d9d3 to your computer and use it in GitHub Desktop.
Save micronax/fa4ee3e2f920fcc6d9d3 to your computer and use it in GitHub Desktop.
Symfony2 Choice Form Validator for constraining a minimum selection of choices without using an entity
<?php
$form = $this->createFormBuilder()
->add('test_choice', 'choice', array(
'expanded' => true,
'multiple' => true,
'constraints' => array(new MinChoices(array('min' => 1))), // Require a minimum of 1 selection
'choices' => array(
'environmental_science' => 'environmental_science',
'energy' => 'energy',
'international_trade' => 'international_trade',
'travel_tourism' => 'travel_tourism',
'restaurant_gastronomy' => 'restaurant_gastronomy',
'food_drink' => 'food_drink',
'health_wellbeing_medicine' => 'health_wellbeing_medicine',
'education' => 'education',
'personal_development' => 'personal_development',
'language_culture' => 'language_culture',
'fashion_beauty' => 'fashion_beauty',
'animals_veterinary' => 'animals_veterinary',
'other' => 'other',
)
))
->add('submit', 'submit', array(
'label' => 'Speichern',
'attr' => array('class' => 'btn btn-primary pull-right')
))
->getForm();
<?php
namespace AppBundle\Validator;
use Symfony\Component\Validator\Constraint;
class MinChoices extends Constraint
{
const TOO_FEW_ERROR = 2;
const TOO_MANY_ERROR = 3;
protected static $errorNames = array(
self::TOO_FEW_ERROR => 'TOO_FEW_ERROR',
self::TOO_MANY_ERROR => 'TOO_MANY_ERROR',
);
public $min;
public $max;
public $minMessage = 'You must select at least {{ limit }} choice.|You must select at least {{ limit }} choices.';
public $maxMessage = 'You must select at most {{ limit }} choice.|You must select at most {{ limit }} choices.';
/**
* {@inheritdoc}
*/
public function getDefaultOption()
{
return 'choices';
}
}
<?php
namespace AppBundle\Validator;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
class MinChoicesValidator extends ConstraintValidator
{
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof MinChoices) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\MinChoices');
}
if (null === $value) {
return;
}
$count = count($value);
if ($constraint->min !== null && $count < $constraint->min) {
$this->buildViolation($constraint->minMessage)
->setParameter('{{ limit }}', $constraint->min)
->setPlural((int) $constraint->min)
->setCode(MinChoices::TOO_FEW_ERROR)
->addViolation();
return;
}
if ($constraint->max !== null && $count > $constraint->max) {
$this->buildViolation($constraint->maxMessage)
->setParameter('{{ limit }}', $constraint->max)
->setPlural((int) $constraint->max)
->setCode(MinChoices::TOO_MANY_ERROR)
->addViolation();
return;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment