问题描述
我有这种形式(我没有给出所有的恶魔,因为它们太多了),但是逻辑是一样的:
$builder
->add('society',EntityType::class,[
'class' => Society::class,'label' => 'society','choice_label' => 'legalName','required' => true,'attr' => [
'data-select' => true,],'placeholder' => '-- choose society --','empty_data' => ''
])
->add('employerLegalName',TextType::class,[
'label' => 'employerLegalName','empty_data' => ''
])
;
$formEmployerModifier = function (FormInterface $form,Society $society = null) {
if ($society) {
$form
->remove('employerLegalName')
->add('employerLegalName',[
'label' => 'employerLegalName','empty_data' => $society->getLegalName() ? $society->getLegalName() : ''
])
;
}
};
$builder->get('society')->addEventListener(FormEvents::POST_SUBMIT,function (FormEvent $event) use ($formEmployerModifier) {
$formEmployerModifier($event->getForm()->getParent(),$event->getForm()->getData());
});
此方法有效,当用户选择社团(entityType)时,它将在loyalLegalname字段中更新社团的名称。
但是...
@请参阅https://symfony.com/doc/4.4/form/events.html
如果我没看错: 可以使用Post Submit Event:
从数据的规范化表示中更改数据。
并且Pre Submit Event可以用来:
在将数据提交到表单之前,更改请求中的数据
添加或删除表单字段,然后再将数据提交到表单。
但是在我的情况下,我删除并添加了带有提交后的字段(它可以工作),但是当我切换到PreSubmit Event时它不起作用(但是文档说应该这样做)。
我还以为我必须在ployLegalName字段中填写data
才能设置默认值,但是它不起作用,我必须使用empty_data
。
我没有被阻止(代码可以创建一个实体并对其进行编辑),但是我想知道自己是否正确执行
我也提供JS代码,以防万一(也许可以帮助人们):
$(document).ready(function () {
/* UPDATE SOCIETY INFORMATIONS */
let $society = $('#contract_apprentice_society');
$society.change(function() {
let $form = $(this).closest('form');
let data = {};
data[$society.attr('name')] = $society.val();
$.ajax({
url : $form.attr('action'),type: $form.attr('method'),data : data,success: function(html) {
$('#contract_apprentice_employerLegalName').val(
$(html).find('#contract_apprentice_employerLegalName').val()
)
}
)}
)}
})
感谢您的帮助!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)