首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Symfony中删除复选框类型中的特定实体?

如何在Symfony中删除复选框类型中的特定实体?
EN

Stack Overflow用户
提问于 2019-08-05 22:02:03
回答 1查看 192关注 0票数 1

我有一个表单,我想在其中添加或删除集合中的元素的复选框:一个具有Responsability[]User

我想在表单中显示一些现有的Responsability,但不是全部。我使用一个名为automatic的属性来确定是否要显示它们。

我如何编辑我的表单来做这样的事情?

UserType.php

代码语言:javascript
复制
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
            ->add('username', TextType::class, [
                'label' => 'Nom d\'utilisateurice'
            ])
            ->add('responsibilities', EntityType::class, [
                // looks for choices from this entity
                'class' => Responsibility::class,
                // uses the Responsibility.label property as the visible option string
                'choice_label' => 'label',
                'label' => 'Rôles',
                'multiple' => true,
                'expanded' => true,
                'choice_attr' => function($responsibility)
                {
                    return [
                        'data-responsibility-description' => $responsibility->getDescription(),
                    ];
                },
            ])
            ->add('submit',SubmitType::class, [
            'label' => 'Changer les informations',
            'attr' => [
                'class' => 'btn btn-outline-primary float-right'
                ]
            ]);
}

edit.html.twig

代码语言:javascript
复制
{{ form_start(edit_form, {'attr': {'id': 'form-edit-user'}}) }}
    <div class="form-group">
        {{ form_label(edit_form.username) }}
        {{ form_widget(edit_form.username) }}
    </div>
    <div class="form-group">
        {{ form_label(edit_form.responsibilities) }}
        {% for responsibility in edit_form.responsibilities %}
                <div class="form-group">
                    {{ form_widget(responsibility) }}
                    {{ form_label(responsibility) }}
                    <span class="text-muted responsibility-description">
                        {{ responsibility.vars.attr['data-responsibility-description'] }}
                    </span>
                </div>
        {% endfor %}
    </div>
    {{ form_widget(edit_form) }}
{{ form_end(edit_form) }}
EN

回答 1

Stack Overflow用户

发布于 2019-08-06 02:34:13

您可以将query_builder表单选项用作文档化的here

像这样的东西应该是有效的:

代码语言:javascript
复制
'query_builder' => function (EntityRepository $repository) {
    return $repository
        ->createQueryBuilder('o')
        ->where('o.automatic = FALSE');
}

或者,如果您更喜欢使用参数,则如下所示:

代码语言:javascript
复制
'query_builder' => function (EntityRepository $repository) {
    return $repository
        ->createQueryBuilder('o')
        ->where('o.automatic = :automatic')
        ->setParameter('automatic', false);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57360200

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档