Created
August 17, 2015 23:38
-
-
Save weaverryan/fefa3a79be3ec2a96ba3 to your computer and use it in GitHub Desktop.
Symfony 2.7 OptionsResolver version of the is_edit option
This file contains hidden or 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 AppBundle\Form; | |
| use Symfony\Component\Form\AbstractType; | |
| use Symfony\Component\Form\FormBuilderInterface; | |
| use Symfony\Component\OptionsResolver\OptionsResolver; | |
| use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
| class ProgrammerType extends AbstractType | |
| { | |
| public function buildForm(FormBuilderInterface $builder, array $options) | |
| { | |
| $builder | |
| ->add('nickname', 'text', [ | |
| // readonly if we're in edit mode | |
| 'disabled' => $options['is_edit'] | |
| ]) | |
| ->add('avatarNumber', 'choice', [ | |
| 'choices' => [ | |
| // the key is the value that will be set | |
| // the value/label isn't shown in an API, and could | |
| // be set to anything | |
| 1 => 'Girl (green)', | |
| 2 => 'Boy', | |
| 3 => 'Cat', | |
| 4 => 'Boy with Hat', | |
| 5 => 'Happy Robot', | |
| 6 => 'Girl (purple)', | |
| ] | |
| ]) | |
| ->add('tagLine', 'textarea') | |
| ; | |
| } | |
| public function configureOptions(OptionsResolver $resolver) | |
| { | |
| $resolver->setDefaults(array( | |
| 'data_class' => 'AppBundle\Entity\Programmer', | |
| 'is_edit' => false, | |
| )); | |
| } | |
| public function getName() | |
| { | |
| return 'programmer'; | |
| } | |
| } |
This file contains hidden or 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 AppBundle\Form; | |
| use Symfony\Component\Form\AbstractType; | |
| use Symfony\Component\Form\FormBuilderInterface; | |
| use Symfony\Component\OptionsResolver\OptionsResolver; | |
| use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
| class UpdateProgrammerType extends ProgrammerType | |
| { | |
| public function configureOptions(OptionsResolver $resolver) | |
| { | |
| parent::configureOptions($resolver); | |
| // override this! | |
| $resolver->setDefaults(['is_edit' => true]); | |
| } | |
| public function getName() | |
| { | |
| return 'programmer_edit'; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment