简体   繁体   English

如何在symfony 2表单中添加一些额外的数据

[英]How to add some extra data to a symfony 2 form

I have a form for my entity called Book and I have a type to display a form in my view. 我有一个名为Book实体表单,我有一个类型可以在我的视图中显示表单。 In this type I have some fields that are mapped to properties in my entity. 在这种类型中,我有一些字段映射到我的实体中的属性。

Now I want to add another field which is not mapped in my entity and supply some initial data for that field during form creation. 现在我想添加另一个未映射到我的实体中的字段,并在表单创建期间为该字段提供一些初始数据。

My Type looks like this 我的类型看起来像这样

// BookBundle\Type\Book
public function buildForm(FormBuilderInterface $builder, array $options = null)
{
    $builder->add('title');
    $builder->add('another_field', null, array(
        'mapped' => false
    ));
}

The form is created like this 表单就是这样创建的

$book = $repository->find(1);
$form = $this->createForm(new BookType(), $book);

How can I supply some initial data now during form creation? 如何在表单创建过程中提供一些初始数据? Or how do I have to change that creation of the form to add initial data to the another_field field? 或者我如何更改表单的创建以将初始数据添加到another_field字段?

I also have a form that has fields that mostly match a previously defined entity, but one of the form fields has mapped set to false. 我还有一个表单,其中的字段大多与先前定义的实体匹配,但其中一个表单字段已将映射设置为false。

To get around this in the controller, you can give it some initial data pretty easily like this: 要在控制器中解决这个问题,你可以很容易地给它一些初始数据:

$product = new Product(); // or load with Doctrine/Propel
$initialData = "John Doe, this field is not actually mapped to Product";
$form = $this->createForm(new ProductType(), $product);
$form->get('nonMappedField')->setData($initialData);

simple as that. 就那么简单。 Then when you're processing the form data to get ready to save it, you can access the non-mapped data with: 然后,当您处理表单数据以准备保存它时,您可以使用以下命令访问非映射数据:

$form->get('nonMappedField')->getData();

One suggestion might be to add a constructor argument (or setter) on your BookType that includes the "another_field" data, and in the add arguments, set the 'data' parameter: 一个建议可能是在BookType上添加一个包含“another_field”数据的构造函数参数(或setter),并在add参数中设置'data'参数:

class BookType 
{
    private $anotherFieldValue;

    public function __construct($anotherFieldValue)
    {
       $this->anotherFieldValue = $anotherFieldValue;
    }

    public function buildForm(FormBuilderInterface $builder, array $options = null)
    {
        $builder->add('another_field', 'hidden', array(
            'property_path' => false,
            'data' => $this->anotherFieldValue

        )); 
    }
}

Then construct: 然后构造:

$this->createForm(new BookType('blahblah'), $book);

You can change the request parameters like this to support the form with additional data: 您可以更改此类请求参数以支持包含其他数据的表单:

$type = new BookType();

$data = $this->getRequest()->request->get($type->getName());
$data = array_merge($data, array(
    'additional_field' => 'value'
));

$this->getRequest()->request->set($type->getName(), $data);

This way your form will fill in the correct values for your field at rendering. 这样,您的表单将在渲染时为您的字段填充正确的值。 If you want to supply many fields this may be an option. 如果您想提供许多字段,这可能是一个选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM