【问题标题】:how to delete embedded form (collection field) label in symfony如何在 symfony 中删除嵌入式表单(集合字段)标签
【发布时间】: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>

它在我的浏览器上的 html 视图中呈现这个:

如您所见,我有一个名为 __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) }},我的字段不会显示在我的视图中。

标签: php forms symfony twig


【解决方案1】:

我认为你有这些标签是因为你使用了 symfony 预定义的默认视图格式你需要自定义它,另一个原因是你已经显示了嵌入的表单原型,你需要将此原型设置为数据类型属性:

<ul class="tags" data-prototype="{{ form_widget(form.tags.vars.prototype)|e }}">
...

见http://symfony.com/doc/current/cookbook/form/form_collections.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-12
    • 1970-01-01
    • 2016-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多