访问Symfony2 Controller中未映射的字段

时间:2022-10-16 18:01:29

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:

但是当我想在控制器或类似设备中访问它时,目前我正在使用POST请求数组并从那里出来,如下所示:

$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?

有比这更好的方法吗?

2 个解决方案

#1


60  

You can access unmapped field in form

您可以在表单中访问未映射的字段

$unmappedField = $form['unmapped_field']->getData();

#2


14  

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

取自symfony doc sf 2.5(也用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)

http://symfony.com/doc/current/book/forms.html#creating-form-classes(向下滚动一点)

#1


60  

You can access unmapped field in form

您可以在表单中访问未映射的字段

$unmappedField = $form['unmapped_field']->getData();

#2


14  

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

取自symfony doc sf 2.5(也用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)

http://symfony.com/doc/current/book/forms.html#creating-form-classes(向下滚动一点)