Skip to content

Instantly share code, notes, and snippets.

@webmozart
Forked from romainneutron/gist:7596954
Last active December 29, 2015 01:59
Show Gist options
  • Save webmozart/7597726 to your computer and use it in GitHub Desktop.
Save webmozart/7597726 to your computer and use it in GitHub Desktop.
<?php
use Symfony\Component\Form\Forms;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ApplicationFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('title', 'text', array(
'label' => 'Application title',
));
$builder->add('description', 'text', array(
'label' => 'Application description',
));
$builder->setData(array(
'title' => 'A default title',
'description' => 'A default description',
));
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use (&$defaultData) {
$defaultData = $event->getForm()->getData();
var_dump($defaultData);
}, 255);
}
public function getName()
{
return 'application';
}
}
$factory = Forms::createFormFactoryBuilder()->getFormFactory();
$form = $factory->create(new ApplicationFormType());
$form->submit(array());
// expecting array('title' => 'A defaut title', 'description' => 'A defaut description') but got null
var_dump($form->getData());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment