Skip to content

Instantly share code, notes, and snippets.

@mattsches
Created June 16, 2012 14:02
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 mattsches/2941407 to your computer and use it in GitHub Desktop.
Save mattsches/2941407 to your computer and use it in GitHub Desktop.
EventListener mit PRE_SET_DATA-Event in Symfony2
/* ... */
public function buildForm(FormBuilder $builder, array $options)
{
$builder->addEventListener(FormEvents::PRE_SET_DATA,
function (DataEvent $event) use ($builder)
{
$form = $event->getForm();
/* @var \Sperrobjekt\Sf2ttBundle\Entity\Category $data */
$data = $event->getData();
if ($data instanceof \Sperrobjekt\Sf2ttBundle\Entity\Category) {
$label = $data->getName();
$form->add($builder->getFormFactory()->createNamed(
'text',
'Category Name',
null,
array(
'label' => $label,
)
));
}
});
}
/* ... */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment