Symfony 3使用表单更新角色用户

时间:2016-10-09 15:17:09

标签: php twig symfony symfony-forms

我需要一些帮助,我会创建一个表单来更改应用中用户的角色。

我正在使用Symfony 3.1.X的“默认”捆绑安全性。

实际上我可以检索数据库中的所有角色并显示它们,我也对数据做了一些处理,但我仍然坚持显示问题,当我显示表单时,Twig包继续显示一些标签,已经显示在下面。

我该如何解决这个问题?

这是我的代码:

显示模板

    {% 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 %}

我的表格是

<?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
        ));
    }
}

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

Wel我终于找到了,我从TWIG模板中删除了条件,它工作正常,我有我想要的,它需要的地方!

我在这里发布用于解决问题的最终代码:

TWIG模板:

{% 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 %}

表单代码:

<?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
        ));
    }
}

这里是调用模板的方法,以及表单构造:

<?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]);
    }