Skip to content

Instantly share code, notes, and snippets.

@romainneutron
Last active December 29, 2015 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save romainneutron/7596954 to your computer and use it in GitHub Desktop.
Save romainneutron/7596954 to your computer and use it in GitHub Desktop.
Symfony form default data
<?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',
'data' => 'A defaut title',
));
$builder->add('description', 'text', array(
'label' => 'Application description',
'data' => 'A defaut 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