Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access an unmapped field in Symfony2 Controller

Tags:

forms

symfony

I am creating forms with an unmapped field as explained in the form documentation.

However when in the controller or similar I want to access it, currently I am using the POST request array and getting out from there like so:

$postData = $this->getRequest()->request->get('my_form_name');
$unmappedField = $postData['unmapped_field']

I just can't help but thinking this is not the best way, and I cannot find anything on the official documentation.

Is there a better way than this?

like image 936
Andrew Atkinson Avatar asked Jun 13 '13 09:06

Andrew Atkinson


2 Answers

You can access unmapped field in form

$unmappedField = $form['unmapped_field']->getData();
like image 189
Alexey B. Avatar answered Nov 06 '22 18:11

Alexey B.


taken from the symfony doc sf 2.5 (also tested with sf 2.3):

form type:

use Symfony\Component\Form\FormBuilderInterface;

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('task')
        ->add('dueDate', null, array('mapped' => false))


  ->add('save', 'submit');
}

controller:

$form->get('dueDate')->getData();
$form->get('dueDate')->setData(new \DateTime());

http://symfony.com/doc/current/book/forms.html#creating-form-classes (scroll down a little bit)

like image 38
c33s Avatar answered Nov 06 '22 20:11

c33s