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?
You can access unmapped field in form
$unmappedField = $form['unmapped_field']->getData();
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With