如何在Symfony 4中预先选择生成的复选框?

时间:2018-04-16 21:28:11

标签: symfony checkbox symfony-forms

让我们想象我们想要为帖子分配类别,我们使用EntityType根据我们的类别数量生成它们,所以我们只需将下一个代码块添加到表单中:

控制器:

->add('categories', EntityType::class, array(
   'class' => Category::class,
   'choice_label' => 'category_description',
   'multiple' => true,
   'expanded' => true,
   'required' => false,
))

然后在提交表单时将它们保存到数据库,ManyToMany,使用ArrayCollection:

foreach($post_data['categories'] as $form_category)
{
  $database_category = $database_manager->getRepository(Category::class)->find($form_category->getId());
  $post->addCategory($database_category);
}

实体:

    <?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity(repositoryClass="App\Repository\PostRepository")
 */
class Post
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    ...

    /**
     *  @ORM\ManyToMany(targetEntity="Category", cascade={"persist"})
     *  @ORM\JoinTable(name="junction_table",
     *      joinColumns={@ORM\JoinColumn(name="post_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="category_id", referencedColumnName="id")}
     *   )
     */
    private $categories;

    public function __construct() {
        $this->categories = new ArrayCollection();
    }

    ...

    public function getCategories()
    {
        return $this->categories;
    }

    public function addCategory(Category $category): self
    {
        $this->categories->add($category);

        return $this;
    }
}

图片:

Generated checkboxes with description and index

但是,如果我想在我进入编辑模式时预先选择这些复选框,以便用户知道上次选择了哪些复选框,那你会怎样接近呢?

如果取消选择,你将如何删除一个?

1 个答案:

答案 0 :(得分:1)

如果表单字段映射到实体,则应自动设置数据。 您甚至不必手动将类别添加到帖子中。

以下是与您要执行的操作相匹配的完整工作代码示例:

//src/Entity/Group.php
namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\GroupRepository")
 * @ORM\Table(name="`group`")
 */
class Group
{
    /**
     * Group constructor.
     */
    public function __construct()
    {
        $this->players = new ArrayCollection();
    }

    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Player", inversedBy="groups")
     */
    private $players;

    /**
     * @return mixed
     */
    public function getPlayers()
    {
        return $this->players;
    }

    /**
     * @param mixed $players
     *
     * @return Group
     */
    public function setPlayers($players)
    {
        $this->players = $players;

        return $this;
    }

    /**
     * @param Player $player
     *
     * @return Group
     */
    public function addPlayer(Player $player)
    {
        $this->players->add($player);

        return $this;
    }

    /**
     * @param Player $player
     *
     * @return Group
     */
    public function removePlayer(Player $player)
    {
        $this->players->removeElement($player);

        return $this;
    }
}

//src/Entity/Player.php
namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\PlayerRepository")
 */
class Player
{
    /**
     * @return string
     */
    public function __toString()
    {
        return $this->name;
    }

    /**
     * Player constructor.
     */
    public function __construct()
    {
        $this->groups = new ArrayCollection();
    }

    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Group", mappedBy="players")
     */
    private $groups;

    /**
     * @return mixed
     */
    public function getGroups()
    {
        return $this->groups;
    }

    /**
     * @param mixed $groups
     *
     * @return Player
     */
    public function setGroups($groups)
    {
        $this->groups = $groups;

        return $this;
    }

    /**
     * @param Group $group
     *
     * @return Player
     */
    public function addGroup(Group $group)
    {
        $this->groups->add($group);

        return $this;
    }

    /**
     * @param Group $group
     *
     * @return Player
     */
    public function removeGroup(Group $group)
    {
        $this->groups->removeElement($group);

        return $this;
    }
}

//src/Form/GroupType.php
namespace App\Form;

use App\Entity\Group;
use App\Entity\Player;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class GroupType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add(
                'players',
                EntityType::class,
                [
                    'class' => Player::class,
                    'multiple' => true,
                    'expanded' => true,
                    'required' => false,
                ]
            );
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(
            [
                'data_class' => Group::class,
            ]
        );
    }
}

//src/Controller/GroupController.php
namespace App\Controller;

use App\Entity\Group;
use App\Form\GroupType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class GroupController extends Controller
{
    /**
     * @Route("/edit-group/{id}", name="group_edit")
     * @param Request $request
     * @param Group $group
     *
     * @return Response
     */
    public function editGroup(Request $request, Group $group)
    {
        $em = $this->getDoctrine()->getManager();
        $form = $this->createForm(GroupType::class, $group);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {

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

        return new Response(
            $this->renderView(
                'group/edit-group.html.twig',
                array(
                    'form' => $form->createView(),
                    )
            )
        );
    }
}

//templates/group/edit-group.html.twig
{% extends 'base.html.twig' %}

{% block body %}
<div class="container">
    <div class="row">
        <h1>Edit group</h1>
    </div>

    {{ form_start(form) }}

    {{ form_row(form.players) }}

    <button type="submit" class="btn btn-success">Edit</button>

    {{ form_rest(form) }}
    {{ form_end(form) }}
</div>
{% endblock %}