我想嵌入一个预充电的非实体表单的集合,这里是代码,首先是父表单的buildForm方法。
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add("example1")->add("example2");
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
/*some logic to do before adding the collection of forms*/
$form->add('aclAccess', 'collection', array(
'type' => new ChildFormType(),
'allow_add' => true,
'mapped' => false,
'data' => /* I dont know how to precharge a collection of non-entity forms*/
));
});
}
现在是子窗体
public function buildForm (FormBuilderInterface $builder, array $options) {
$builder->add("test1", "text", array("read_only" => true, "data" => "test"));
$builder->->add("test2", "choice", array(
'choices' => array('opt1' => 'Opt1', 'opt2' => 'Opt2'),
'multiple' => true,
'expanded' => true
));
}
所以基本上我想将 test2 字段中的那些子选项作为单独的表单来管理,每个选项组将取决于 test1 字段的值,我知道这可以通过在没有表单类的情况下在 twig 中编码所有内容来完成,但我认为有表单类它是运行 phpunit 测试、可维护性等的最佳实践......