我一直在使用Symfony,我正在尝试找出一种创建表单的方法。我需要使用基于 MVC 的解决方案。
我的表单需要询问不同实体的多个信息,然后我需要处理该信息并将其提取到数据库中。数据库不会有问题。
我只是在弄清楚如何制作具有不同类型实体的表单?
以及如何使用实体数据库中包含的数据制作向下滚动菜单?
如果 @chalasr 的注释不适用,即实体不相关,则可以在控制器中执行以下操作。 只需为每个实体{x}类型表单创建一个$form变量,如下所示:
$formA = $this->createForm(AppBundle'Entity'EntityAType($entityA));
$formB = $this->createForm(AppBundle'Entity'EntityBType($entityB));
...
return array(
'formA' => $formA->createView(),
'formB' => $formB->createView(),
...
);
您可以简单地组合表单,同时保持每个表单分开,就像这样
namespace AppBundle'Form;
use Symfony'Component'Form'AbstractType;
use Symfony'Component'Form'FormBuilderInterface;
use Symfony'Component'OptionsResolver'OptionsResolver;
class ReallyBigFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('foo', FooType::class, [
// label, required, ... as well as options of FooType
])
->add('bar', BarType::class, [
// label, required, ... as well as options of BarType
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([]);
}
}
并像常规表单一样定义FooType
和BarType
namespace AppBundle'Form;
use Symfony'Component'Form'AbstractType;
use Symfony'Component'Form'FormBuilderInterface;
use Symfony'Component'OptionsResolver'OptionsResolver;
use Symfony'Component'Form'Extension'Core'Type'TextType
class FooType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class, [
'label' => 'foo.name',
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => 'AppBundle'Entity'Foo',
]);
}
}