Last active
August 29, 2015 14:06
-
-
Save maximecolin/4f2ab42c6b4feaa32db4 to your computer and use it in GitHub Desktop.
Use different validation groups depending on which submit button is clicked
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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