我刚刚通过嵌入表单创建了两个表单类型'InformationsType'和'TeamsType'的自定义子项与另一个表单'TrainingsType'相关:
TrainingsType:
use AppBundle\Form\InformationsType;
use AppBundle\Form\TeamsType;
class TrainingsType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('team', TeamsType::class)
->add('information', InformationsType::class);
}
TeamsType:
class TeamsType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('nameInstitue')->add('nameSpace')->add('webSpace')
->add('members', CollectionType::class, [
'entry_type' => Team_membersType::class,
'allow_delete' => true,
'allow_add' => true,
'by_reference' => false,
]);
}
InformationsType:
class InformationsType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('category', ChoiceType::class, array(
'choices' => array(
'Formation' => 'Francais',
'Certification' => 'Certification',
'Tutoriel' => 'Tutoriel',
'Conférence' => 'Conférence',
'Atelier' => 'Atelier',
'Webinar' => 'Webinar',
'Meet-up' => 'Meet-up',
),
))->add('language', ChoiceType::class, array(
'choices' => array(
'Francais' => 'Francais',
'Anglais' => 'Anglais',
),
))->add('eventType', ChoiceType::class, array(
'choices' => array(
'Permanant' => 'Permanant',
'Session' => 'Session',
),
))->add('priceDescription')->add('title')->add('webAdress')->add('description')->add('priceRadio')->add('validationType')
->add('trainingPrice',ChoiceType::class, array(
'choices' => array('Gratuit'=>'Gratuit','Payant'=>'Payant','Abonnement'=>'Abonnement'),'multiple'=>false,'expanded'=>true))
->add('isCheckedPiece', CheckboxType::class, array(
'required' => false,
));
}
当我通过{{form(form)}}呈现训练表单时,每件事都有效。 但在我的情况下,我需要循环form.team.members,因为它是一个带有添加和删除选项的集合类型表单, 但是这里
我无法访问团队属性的问题({{form(form.team)}}, 它不存在!!
我试图转储(表单),我得到这个Formview对象,没有任何团队属性,因为我通过{{form(form)}}获得了我的表格:
FormView {#365 ▼
+vars: array:24 [▼
"value" => Informations {#313 ▶}
"attr" => []
"form" => FormView {#365}
"id" => "appbundle_informations"
"name" => "appbundle_informations"
"full_name" => "appbundle_informations"
"disabled" => false
"label" => null
"label_format" => null
"multipart" => false
"block_prefixes" => array:3 [▶]
"unique_block_prefix" => "_appbundle_informations"
"translation_domain" => null
"cache_key" => "_appbundle_informations_appbundle_informations"
"errors" => FormErrorIterator {#555 ▶}
"valid" => true
"data" => Informations {#313 ▶}
"required" => true
"size" => null
"label_attr" => []
"compound" => true
"method" => "POST"
"action" => ""
"submitted" => false
]
+parent: null
+children: array:12 [▼
"category" => FormView {#610 ▶}
"language" => FormView {#641 ▶}
"eventType" => FormView {#659 ▶}
"priceDescription" => FormView {#668 ▶}
"title" => FormView {#670 ▶}
"webAdress" => FormView {#672 ▶}
"description" => FormView {#674 ▶}
"priceRadio" => FormView {#676 ▶}
"validationType" => FormView {#678 ▶}
"trainingPrice" => FormView {#680 ▶}
"isCheckedPiece" => FormView {#682 ▶}
"_token" => FormView {#696 ▶}
]
-rendered: false
-methodRendered: false
}
任何帮助,任何解释!!
答案 0 :(得分:1)
我认为您想要使用的是
attribute(form, team)
请参阅https://twig.symfony.com/doc/2.x/functions/attribute.html
编辑:
您的转储看起来像是MessagesType的转储,而不是TrainingsType,您确定使用的是正确的表单吗?