Render a filtered checkbox collection in a form

老子叫甜甜 提交于 2020-01-05 03:36:42

问题


I want to render a filtered collection as a checkbox list. But i have trouble to get the collection shown. i get "Catchable Fatal Error: Object of class Doctrine\ORM\PersistentCollection could not be converted to string".

Below is my formtype:

class PropertyfilterType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('view', EntityType::class, [
                'class' => Propsearch::class,
                'choice_label' => 'propsearchviews',
               'expanded' => true,
                'multiple' => true

            ]);
}

This is my many-to-many entity

<?php

namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;


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



   /**
     * @var Propsearchview[]|ArrayCollection
     *
     * @ORM\ManyToMany(targetEntity="App\Entity\Propview", cascade={"persist"})
     * @ORM\JoinTable(name="propsearch_propview")
     * @ORM\OrderBy({"title": "ASC"})
     * @Assert\Count(max="4", maxMessage="Can only select 4 views")
     */
    private $propsearchviews;



   /**
     * @var Propsearchfacility[]|ArrayCollection
     *
     * @ORM\ManyToMany(targetEntity="App\Entity\Propfacility", cascade={"persist"})
     * @ORM\JoinTable(name="propsearch_propfacility")
     * @ORM\OrderBy({"title": "ASC"})
     * @Assert\Count(max="4", maxMessage="Can only select 4 facilities")
     */
    private $propsearchfacilities;



    public function getId(): ?int 
    {
        return $this->id;
    }




    public function __construct()
    {

        $this->propsearchviews = new ArrayCollection();
        $this->propsearchfacilities = new ArrayCollection();
    }




   /**
     * @return Collection|Propsearchview[]
     */
    public function getPropsearchviews(): Collection
    {
        return $this->propsearchviews;
    }



    public function addPropsearchview(Propsearchview $propsearchview): self
    {
        if (!$this->propsearchviews->contains($propsearchview)) {
            $this->propsearchviews[] = $propsearchview;
        }
        return $this;
    }



    public function removePropsearchview(Propsearchview $propsearchview): self
    {
        if ($this->propsearchviews->contains($propsearchview)) {

            $this->propsearchviews->removeElement($propsearchview);
        }
        return $this;
    }






    /**
     * @return Collection|Propsearchfacility[]
     */
    public function getPropsearchfacilities(): Collection
    {
        return $this->propsearchfacilities;
    }


    public function addPropsearchfacility(Propsearchfacility $propsearchfacility): self
    {
        if (!$this->propsearchfacilities->contains($propsearchfacility)) {

            $this->propsearchfacilities[] = $propsearchacility;
        }
        return $this;
    }



    public function removePropsearchfacility(Propsearchfacility $propsearchfacility): self
    {
        if ($this->propsearchfacilities->contains($propsearchfacility)) {

            $this->propsearchfacilities->removeElement($propsearchfacility);
        }
        return $this;
    }





}

This is my original view entity.

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity()
 * @ORM\Table(name="propview")
 *
 * Defines the properties of the Tag entity to represent the post tags.
 *
 * See https://symfony.com/doc/current/book/doctrine.html#creating-an-entity-class
 *
 * @author Yonel Ceruto <yonelceruto@gmail.com>
 */
class Propview 
{
    /**
     * @var int
     *
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;


    /**
     * @var string
     *
     * @ORM\Column(type="string", length=191)
     */
    private $title;




    public function getId(): ?int
    {
        return $this->id;
    }


    public function getTitle(): ?string
    {
        return $this->title;
    }


    public function setTitle(string $title): self
    {
        $this->title = $title;

        return $this;
    }



    public function __toString(): string
    {

        return $this->title;
    }



}

So i want to show the collection of views as a checkbox list that has been added to the propsearch table in the form. Thanks in advance!


Edit 2 Okay so i have the propsearchviews which has an colleciton from propviewtype. including the dataclass from propsearch.

I changed my propertyfiltertype to the following:

    <?php

    namespace App\Form;

    use App\Entity\Propsearch;

    class PropertyfilterType extends AbstractType
    {
            public function buildForm(FormBuilderInterface $builder, array $options)
            {
                $builder
        ->add('propsearchviews', CollectionType::class, [
            'entry_type' => PropviewType::class,
            'by_reference' => false,
        ]);




}

the propviewtype itself

namespace App\Form\Type;

use App\Entity\Propview;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

use Symfony\Bridge\Doctrine\Form\Type\EntityType;

class PropviewType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{


    $builder

        ->add('propview', EntityType::class, [
        'class' => Propview::class,
        'choice_label' => 'title',

        ]);



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

}

and my html.twig file

<div class="col-12 col-md-4 mb-2">


{% for field in propertybuyform.propsearchviews %}
    <div class="col-xs-4">
        {{ form_widget(field) }}
        {{ form_label(field) }}
    </div>
{% endfor %}



</div>

回答1:


You should use embedded form functionality to achieve this. Please refer to https://symfony.com/doc/current/form/form_collections.html to get idea of how it could be implemented. Briefly describing your case - you should create PropsearchType which would render propsearchviews property as CollectionType, where 'entry_type' would be another custom form type that you should create - PropviewType, that should render your Propviews as checboxes.



来源:https://stackoverflow.com/questions/55864386/render-a-filtered-checkbox-collection-in-a-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!