php 如何向 symfony 2 表单添加一些额外数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14223204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:52:36  来源:igfitidea点击:

How to add some extra data to a symfony 2 form

phpformssymfonyentity

提问by Benjamin Paap

I have a form for my entity called Bookand I have a type to display a form in my view. In this type I have some fields that are mapped to properties in my entity.

我有一个名为我的实体的表单,我Book有一个类型可以在我的视图中显示一个表单。在这种类型中,我有一些字段映射到我的实体中的属性。

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

我现在如何在表单创建过程中提供一些初始数据?或者我必须如何更改表单的创建以将初始数据添加到another_field字段?

回答by targnation

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();

回答by Mike

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:

一个建议可能是在包含“another_field”数据的 BookType 上添加一个构造函数参数(或 setter),并在添加参数中设置“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);

回答by Benjamin Paap

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.

这样,您的表单将在呈现时为您的字段填写正确的值。如果您想提供许多字段,这可能是一个选项。