【发布时间】:2015-12-28 14:09:32
【问题描述】:
我的 symfony 应用程序中有这个表格:
namespace MyNamespace\EntityBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class OrganizationType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
// profession checkboxes imbrication
->add('professions', 'collection', array(
'type' => new ProfessionType(),
'allow_add' => true,// if unrecognized items are submitted to the collection, they will be added as new items
'allow_delete' => false,
'by_reference' => false, //in order that the adders are called.
'mapped' => true,
))
->add('name')
->add('siret')
->add('corporation')
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'MyNamespace\EntityBundle\Entity\Organization',
'csrf_protection' => true,
'csrf_field_name' => '_token_',
// a unique key to help generate the secret token
'intention' => 'organization_stuff',
));
}
/**
* @return string
*/
public function getName()
{
return 'organization';
}
}
这就是我在树枝视图中呈现表单的方式:
<div>
{{ form_start(form, {'action': path('path_action'), 'method': 'POST'}) }}
{{ form_errors(form) }}
{{ form_row(form.professions.vars.prototype) }}
{{ form_row(form.name) }}
{{ form_row(form.siret) }}
{{ form_row(form.corporation) }}
{{ form_end(form) }}
</div>
如您所见,我有一个名为 __name__label__ 的必需标签(在表单顶部),并且在提交按钮上方嵌入了表单标签 Professions。
我该如何解决这个问题,或者自定义这个行为?
注意:如果我只使用{{ form_row(form.professions) }},在我的树枝中,我的professionalType 不会显示这些字段。
这是ProfessionType.php的代码:
$builder
->add('production', 'checkbox', array('required' => false ))
->add('transport', 'checkbox', array('required' => false ))
->add('pumping', 'checkbox', array('required' => false ))
;
【问题讨论】:
-
你为什么不把这些职业直接称为:{{ form_row(form.professions) }}?你能告诉我们 ProfessionType 的代码吗?
-
@jiboulex 查看我的编辑,我添加 ProfessionType,并注意我的笔记,如果我只添加
{{ form_row(form.professions) }},我的字段不会显示在我的视图中。