Last active
December 29, 2015 01:59
-
-
Save romainneutron/7596954 to your computer and use it in GitHub Desktop.
Symfony form default data
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 | |
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