Skip to content

Instantly share code, notes, and snippets.

@zyphlar
Last active February 21, 2016 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zyphlar/167de651aa6e431431e0 to your computer and use it in GitHub Desktop.
Save zyphlar/167de651aa6e431431e0 to your computer and use it in GitHub Desktop.
<?php
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\AbstractType;
class MyFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
// ...
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$form = $event->getForm();
$form->add('myField', null, array(
'data' => $myFieldData,
// ...
<?php
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\AbstractType;
class MyFormType extends AbstractType
{
private $myFieldOptions = array();
public function buildForm(FormBuilderInterface $builder, array $options)
{
// ...
$form->add('myField', null, array(
'options' => $this->myFieldOptions,
// ...
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$myObject = $event->getData();
$this->myFieldOptions = ($myObject->isGood() ? array('good', 'super') : array('bad', 'horrible'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment