Skip to content

Instantly share code, notes, and snippets.

@maximecolin
Last active August 29, 2015 14:06
Show Gist options
  • Save maximecolin/4f2ab42c6b4feaa32db4 to your computer and use it in GitHub Desktop.
Save maximecolin/4f2ab42c6b4feaa32db4 to your computer and use it in GitHub Desktop.
Use different validation groups depending on which submit button is clicked
<?php
namespace Acme\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class FoobarType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('foobar', 'text')
->add('barfoo', 'text')
->add('refresh', 'submit')
->add('save', 'submit');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'validation_groups' => function(FormInterface $form) {
// If refresh button is clicked, use "Refresh" validation group
if ($form->get('refresh')->isClicked()) {
return array('Refresh');
}
// Else use "Default" validation group
return array('Default');
}
));
}
public function getName()
{
return 'foobar';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment