maequise maequise - 27 days ago 5
Twig Question

Symfony 3 update roles user with form

I need some help, I would make a form to change the roles of users in the app.

I'm using the "default" bundle security of Symfony 3.1.X.

Actually I can retrieve all the roles in the DB and display them, I also make some treatment on the data, but I still stuck on a display problem, when I display the form, the Twig bundle continues to display some label that already display below.

How can I fix this ?

Here's my code :

display template

{% extends 'admin/base.admin.html.twig' %}

{% block body %}
<section class="container">
{{ user.pseudo }} | 

{{ dump(user, form.roles) }}
{{ form_start(form) }}
{% for role in user.roles %}
{% if form.roles.children[role] is defined %}
{{ form_label(form.roles.children[role]) }}
{{ form_widget(form.roles.children[role], {'attr':{'checked':true}}) }}
{% else %}
{{ form_label(form.roles) }}
{{ form_widget(form.roles) }}
{% endif %}
{% endfor %}
{{ form_end(form) }}
</section>

<div class="well">
{% for key, role in form.roles.children %}
{{ dump(key) }}
{% endfor %}
</div>
{% endblock %}


There's my form

<?php

namespace AppBundle\Form;

use AppBundle\Entity\MembersEntity;
use AppBundle\Entity\RoleEntity;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ChangeRoleForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('roles', EntityType::class, array(
'class' => RoleEntity::class,
'choice_label' => 'nameRole',
'multiple' => true,
'expanded' => true,
'choice_value' => 'nameRole',
))
->add('update', SubmitType::class);
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => null
));
}
}


Any idea ?

Answer

Wel I finally found, I symply deleted the condition from the TWIG template, and it works fine, I have what I want, and where it needs to be !

I post here the final code used to solved the problem :

TWIG template :

{% extends 'admin/base.admin.html.twig' %}

{% block body %}
    <section class="container well">
        {{ user.pseudo }} | 

        {{ form_start(form) }}
        {{ form_widget(form) }}
        {{ form_end(form) }}
    </section>
{% endblock %}

The form code :

<?php

namespace AppBundle\Form;

use AppBundle\Entity\RoleEntity;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class RoleForm extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name_role', TextType::class, array(
            'label' => 'Nom du rôle',
            'label_attr' => array(
                'class' => 'input-group'
            ),
            'attr' => array(
                'class' => '',
                'placeholder' => 'Nom du rôle'
            )
        ))
            ->add('submit', SubmitType::class, array(
                'label' => 'Ajouter '
            ))->setMethod($options['method']);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => RoleEntity::class
        ));
    }
}

And here the method to call the template, and the form construct :

<?php 

public function editMemberAction($id, Request $request)
    {
        $em = $this->getDoctrine()->getManager();
        $user = $this->getDoctrine()->getRepository(MembersEntity::class)->find($id);
        $form = $this->createForm(ChangeRoleForm::class, $user, array('method' => 'PUT'));

        if($request->isMethod('PUT'))
        {
            $form->handleRequest($request);

            $roles = $form->get('roles')->getData();

            $data = array();

            foreach($roles as $role)
            {
                $data[] = $role->getNameRole();
            }

            $user->setRoles($data);

            $em->persist($user);
            $em->flush();
        }

        return $this->render('admin/edit-role-member.html.twig', ['form' => $form->createView(), 'user' => $user]);
    }