0

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?

2
  • 1
    try to add this constraint to your entity, not form Commented Jul 24, 2014 at 18:40
  • Thank you that works perfectly. If I could accept your comment as an answer then I would. Commented Jul 24, 2014 at 19:30

1 Answer 1

1

You need to add this constraint to your entity object, not a form.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.