Skip to content

Instantly share code, notes, and snippets.

@webmozart
Created September 15, 2014 15:54
Show Gist options
  • Select an option

  • Save webmozart/0f08f8326e1804a4934c to your computer and use it in GitHub Desktop.

Select an option

Save webmozart/0f08f8326e1804a4934c to your computer and use it in GitHub Desktop.
<?php
use Doctrine\Common\Annotations\AnnotationRegistry;
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
use Symfony\Component\Form\Forms;
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Validation;
$loader = require_once __DIR__.'/vendor/autoload.php';
AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
class CallbackTest
{
/**
* @param $constraint
* @param ExecutionContextInterface $context
*
* @Callback(groups="MyGroup")
*/
public static function validate($constraint, $context)
{
var_dump('foo');
die;
}
}
$validator = Validation::createValidatorBuilder()
->setApiVersion(Validation::API_VERSION_2_4)
->enableAnnotationMapping()
->getValidator();
$formFactory = Forms::createFormFactoryBuilder()
->addExtension(new ValidatorExtension($validator))
->getFormFactory();
$options = array(
'data_class' => '\CallbackTest',
'validation_groups' => 'MyGroup',
);
$object = new CallbackTest();
$form = $formFactory->createBuilder('form', $object, $options)
->add('foo', 'text', array('mapped' => false))
->getForm();
$form->submit(null);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment