呈现表单中经过过滤的复选框集合 [英] Render a filtered checkbox collection in a form

查看:69
本文介绍了呈现表单中经过过滤的复选框集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将过滤后的集合呈现为复选框列表. 但是我很难得到显示的收藏.我收到可捕获的致命错误:无法将Doctrine \ ORM \ PersistentCollection类的对象转换为字符串".

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".

以下是我的表单类型:

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

            ]);
}

这是我的多对多实体

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



}

所以我想将视图的集合显示为复选框列表,该列表已添加到表单的propsearch表中. 预先感谢!

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!

编辑2 好吧,我有proppropviewviews,它具有propviewtype的colleciton.包括propsearch的数据类.

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

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




}

propviewtype本身

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

}

和我的html.twig文件

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>

推荐答案

您应使用嵌入式表单功能来实现此目的.请参考 https://symfony.com/doc/current/form/form_collections.html 了解如何实现. 简要描述您的情况-您应创建PropsearchType,它将propsearchviews属性呈现为CollectionType,其中"entry_type"是您应创建的另一种自定义表单类型-PropviewType,应将您的Propviews呈现为checboxes.

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.

这篇关于呈现表单中经过过滤的复选框集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆