Skip to content

Instantly share code, notes, and snippets.

@immutef
Last active August 29, 2015 14:04
Show Gist options
  • Save immutef/e144a6feb00ac9ad77ab to your computer and use it in GitHub Desktop.
Save immutef/e144a6feb00ac9ad77ab to your computer and use it in GitHub Desktop.
<?php
namespace Acme\DemoBundle\Model;
use Symfony\Component\Validator\ExecutionContextInterface;
class MyModel
{
public $myProperty;
public function validateProperty(ExecutionContextInterface $context)
{
$context->addViolationAt('myProperty', 'Error');
}
}
<?php
namespace Acme\Bundle\DemoBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints\Callback;
class MyModelType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('myProperty', 'text');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\DemoBundle\Model\MyModel',
'constraints' => array(
new Callback(array(
'callback' => 'validateProperty',
),
),
));
}
public function getName()
{
return 'my_model_form';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment