【发布时间】:2018-06-02 19:25:45
【问题描述】:
我尝试使用CollectionType 在 symfony 3 中构建一个巨大的表单。我必须定义多个子表单,一些是多个,一些是单个。
这是我的 FormType:
public function buildRegistrationForm(FormBuilderInterface $builder, array $options) {
$builder->add('userRegistration', CollectionType::class, [
'entry_type' => UserRegistrationType::class,
'entry_options' => ['label' => true],
]);
$builder->add('meters', CollectionType::class, [
'entry_type' => MeterType::class,
'entry_options' => ['label' => true],
'allow_add' => true,
]);
...
}
现在我尝试访问视图中的 CollectionType 字段。代码如下:
{{ form_label(registrationForm.email, null, {'label_attr': {'class': 'form-label'}}) }}
{{ form_widget(registrationForm.email, {'attr': {'class': 'form-control'}}) }}
但我得到了错误:
Neither the property "email" nor one of the methods "email()", "getemail()"/"isemail()"/"hasemail()" or "__call()" exist and have public access in class "Symfony\Component\Form\FormView".
我知道 Symfony 试图直接从主表单 (registrationForm) 中获取电子邮件字段,但我不知道如何访问子表单。在文档 (http://symfony.com/doc/current/form/form_collections.html) 中描述了我可以使用 registrationForm.userRegistration.email 简单地访问子表单。但这给了我错误:
Neither the property "userRegistration" nor one of the methods ...
如何访问视图中的子字段?
【问题讨论】:
标签: php symfony symfony-forms symfony-3.3