gpt4 book ai didi

php - Symfony 3 - 来自数据库的动态表单字段

转载 作者:行者123 更新时间:2023-11-29 03:23:19 25 4
gpt4 key购买 nike

我在数据库中有一个包含字段的表:- 字段标签、字段类型、字段大小写等

我想通过 field_case 动态地向表单添加字段。

例如:在此表单类型中,我想添加来自数据库的所有字段,其中 field_case = 1

public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add("companie_uid", HiddenType::class);
$builder->add("companie_denumire", TextType::class, array('label' => 'companie_denumire'));
$builder->add("companie_cui", TextType::class, array('label' => 'companie_cui', 'required' => false));
$builder->add("companie_j", TextType::class, array('label' => 'companie_j', 'required' => false));
$builder->add("companie_mail", EmailType::class, array('label' => 'companie_mail', 'required' => false));
$builder->add("companie_website", TextType::class, array('label' => 'companie_website', 'required' => false));
$builder->add("companie_status", HiddenType::class);
$builder->add("companie_descriere", TextAreaType::class, array('label' => 'companie_descriere', 'required' => false));
$builder->add("companie_telefon", TextType::class, array('label' => 'companie_telefon', 'required' => false));
$builder->add("companie_iban", TextType::class, array('label' => 'companie_iban', 'required' => false));
$builder->add("companie_banca", TextType::class, array('label' => 'companie_banca', 'required' => false));
$builder->add("file", FileType::class, array('label' => 'companie_file', 'mapped' => false, 'required' => false));
$builder->add("save", SubmitType::class, array('label' => 'companie_save'));
$builder->add(
$builder->create('address', CompanyAddressType::class, Array('by_reference' => false,))
);
}

我想从表 fields 添加,其中 field_case = 1

$builder->add(field_id, field_type, array('label' => 'field_label'));

并将它们保存到另一个表 field_values 的数据库中。

最佳答案

您可以使用 form listener

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

switch($data->getFieldCase())
{
case 1:
$builder->add(field_id, field_type, array('label' => 'field_label'));
break;
// ...
}
});

关于php - Symfony 3 - 来自数据库的动态表单字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40377877/

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