gpt4 book ai didi

php - Symfony3.4 - POST_SUBMIT 事件的预填充字段

转载 作者:可可西里 更新时间:2023-10-31 22:52:14 26 4
gpt4 key购买 nike

我有一个包含联系人列表的表单。我希望“名字”字段在提交后 显示所选的联系人值。我的问题是该字段出现但我无法设置正确的数据,该字段始终为空。

public function buildForm(FormBuilderInterface $builder, array $options)
{

$builder
->add('contacts', ChoiceType::class, [
'label' => 'Contact',
'placeholder' => 'Choose a contact',
'choices' => $this->getContacts(),
'mapped' => false,
])
->setMethod('POST')
;

$builder->get('contacts')->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {

$contactId = $event->getData();
$parentForm = $event->getForm()->getParent();

$contactEntity = $exampleEm->getrepository(Contact::class)->find($contactId);
$firstName = $contactEntity->getFirstName();

// where can I set the 'contactFirstname' data ?

$parentForm
->add('contactFirstname', TextType::class, [
'label' => 'First name',
]);
})
;
}

如何输入正确的数据以便该字段显示为预填?

编辑:我找到了一个方法,但并不可怕:

$parentForm
->add('contactFirstname', TextType::class, [
'label' => 'First name',
'empty_data' => $firstName,
]);

('data' => $firstName不适合我。)

$parentForm->get('contactFirstname')->setData($firstName); 也不起作用

最佳答案

您不能简单地设置 TextType 字段的“数据”选项吗?

// ...

$contactEntity = $exampleEm->getrepository(Contact::class)->find($contactId);
$firstName = $contactEntity->getFirstName();

$parentForm
->add('contactFirstname', TextType::class, [
'label' => 'First name',
'data' => $firstname //here?
]);

编辑:

根据 this post submitted on github ,需要提交表单字段才能更改其数据。

在他的一个解决方案中,他像您一样使用“empty_data”。

在另一个中,他将字段添加到构建器。使用 display: "none"; 隐藏它,直到数据被提交。

关于php - Symfony3.4 - POST_SUBMIT 事件的预填充字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52836240/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com