2

Could someone help me figure out why my Callback validation method is not being called.

Basically what I need to do is a custom validation with following logic: - in the form I have 5 fields that if all empty, the form should be valid, - however if any of the is not empty all of them need to not be empty (they are used to build a real address on a user profile)

I have followed the doc from: http://symfony.com/doc/2.3/reference/constraints/Callback.html

I have the following code:

/**
 * User
 *
 * @ORM\Table(name="user")
 * @ORM\Entity(repositoryClass="UserRepository");
 * @UniqueEntity("email")
 * @ORM\HasLifecycleCallbacks
 * @Assert\Callback(methods={"isAddressValid"})
 */
class User extends WebserviceUser implements UserInterface, EquatableInterface
{
...

    public function isAddressValid(ExecutionContextInterface $context)
    {
        //die("I GOT HERE");
        $context->addViolationAt('sna4', 'Frikin validation'!', array(), null);
    }
}

The property sna4 is found in the class being extended.

Thank you in advance.

6
  • 1
    did you call isValid() in your controller? code from your controller would help! Commented Mar 13, 2015 at 20:35
  • check you add the use statement for the ExecutionContextInterface class Commented Mar 14, 2015 at 13:52
  • @Matteo..... I confirm including the use Commented Mar 18, 2015 at 14:33
  • @PMoubed...yes I have called isValid code if ($form->isSubmitted() && $form->isValid()) code Commented Mar 18, 2015 at 14:34
  • The form have a validation_groups? Commented Mar 18, 2015 at 15:10

1 Answer 1

4

The callback annotation require (if any is defined) the validation group associated.

As Example, an entity used in different form context with a validation custom for a specific form:

The Entity Class:

/**
 *
 * @Assert\Callback(methods={"validateCommunicationEmail"}, groups={"userProfile"})
 * @Assert\Callback(methods={"validatePreference"}, groups={"userPreference"})
 */
class AcmePreferences
{

    ....

        public function validateCommunicationEmail(ExecutionContextInterface $context)
    {
        if ($this->getIsCommunicationsEnabled() && ! $this->getAdministrativeCommunicationEmail())
        {
            $context->addViolationAt('isCommunicationsEnabled','error.no_administrative_email_selected');
        }

    }

}

The Form Type:

class AcmePreferencesType extends AbstractType
{

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class'=> 'Acme\DemoBundle\Entity\AcmePreferences',
            'validation_groups' => array('userProfile')
        ));
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Matteo, will verify this and get back with an answer asap.
Apparently it has something to do with the form, as I have just received a bug report that on another form that uses the entity, the validation fails and displays the error. Very strange and don't know the reason why, YET.

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.