gpt4 book ai didi

php - 交响乐 3 : set value after submitting a form

转载 作者:可可西里 更新时间:2023-11-01 01:15:46 24 4
gpt4 key购买 nike

我有一个表单集合,我想在提交表单后设置field2,具体取决于field1的值。 field2 在表单中不可编辑,但必需。

我尝试了每个 FormEvent (FormEvents::) 但出现错误:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'field2' cannot be null

// AppBundle/Form/MyFormType.php

// ...
$builder->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $even) {
$data = $event->getData();
$form = $event->getForm();

if (isset($data['field1'])) { // isset
$field2 = $this->container->get('repository')->find($data['field1'])->getValue(); // service works

$data['field2'] = $field2; // !!! doesn't work !!!
$data->setField2($field2); // !!! doesn't work !!!
}
});

我做错了什么?提前致谢!

最佳答案

试试这个

use Symfony\Component\Form\FormEvents;

$builder->addEventListener(FormEvents::PRE_SUBMIT, function(FormEvent $event) {
$data = $event->getData();
$form = $event->getForm();

if (isset($data['field1'])) {
$field2 = $this->container->get('repository')->find($data['field1'])->getValue();

$data['field2'] = $field2;
$event->setData($data);
}
});

关于php - 交响乐 3 : set value after submitting a form,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39205972/

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