I am trying to add a callback constraint to a checkbox in Symfony 2.4. The idea is to check for other values on the object and decided weather or not to allow the validation to pass.
I've got the callback working but the first argument that is returned is not the entity but the value of the checkbox. The Symfony documentation states that the first argument will be the object. http://symfony.com/doc/current/reference/constraints/Callback.html#the-callback-method. I'm not sure what I'm missing
Heres the form code:
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\ExecutionContextInterface;
//...
public function buildForm(FormBuilderInterface $builder, array $options) {
parent::buildForm($builder, $options);
$builder->add('enabled', 'checkbox', array(
'required' => false,
'constraints' => array(
new Assert\Callback(array(
'callback' => array(
$this,
'validateisReady'),
'groups' => $this->validationGroups))
),
))
;
}
public static function validateisReady($object, ExecutionContextInterface $context) {
//..
if($object->getItems()->count() < 1){
$context->addViolationAt('enabled', 'items.missing');
}
//..
}
The $object holds the boolean value of the checkbox. I wanted it to be the entity. Any ideas?