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 ?
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">
{{ user.pseudo }} |
{% set formChil = form.roles.children %}
{{ form_start(form) }}
{% for role in formChil %}
{% if role.vars.value in user.roles %}
{{ form_row(role, {'attr' : {'checked':true}}) }}
{% else %}
{{ form_row(role, {'attr' : {'checked':false}}) }}
{% endif %}
{% endfor %}
{{ form_end(form) }}
</section>
{% endblock %}
The form code :
<?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
));
}
}
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]);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.