1

I have a form in which I want to have checkboxes to add or remove elements from a collection: a User which have Responsability[].

I want to show some of the existing Responsability in the form but not all of them. I use an attribute called automatic to determine if I want to display them or not.

How can I edit my form to do such a thing?

UserType.php:

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:

{{ 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) }}
4
  • 1
    Is the attribute automatic part of the Responsibility-entity and what are the possible values, I guess true/false? Commented Aug 5, 2019 at 14:28
  • autotomatic is a Responsability attribute and has getter and setter. It's possible values are true or false. Commented Aug 5, 2019 at 14:46
  • So, basically you want to only display those Responsibility-entites in your form which have the value set to true and not display any of those with the value false (or maybe null), right? Commented Aug 5, 2019 at 14:54
  • I want to only display the Responsibility-entites in the form which have the value set to false, and not display those with the value true. Commented Aug 5, 2019 at 15:13

1 Answer 1

1

You can use query_builder form option as documented here.

Something like this should work:

'query_builder' => function (EntityRepository $repository) {
    return $repository
        ->createQueryBuilder('o')
        ->where('o.automatic = FALSE');
}

Or like this if you prefer having a parameter:

'query_builder' => function (EntityRepository $repository) {
    return $repository
        ->createQueryBuilder('o')
        ->where('o.automatic = :automatic')
        ->setParameter('automatic', false);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.