Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to choose displayed field of a form after 'choices' findall?

I would like to choose an other field to display in my form checkboxes.

My Entity Filter has three attributes id, name and subtitle.

My code displays my name values, how to display subtitle values ?

My FormBuilder (Controller):

 $formFilter = $this->createFormBuilder()
        ->add('_',     ChoiceType::class,array(
                'choices' => $this->getDoctrine()->getManager()->getRepository('loicFilterBundle:Filter')->findAll(),
                'multiple' => true,
                'expanded' => true,
                'choice_label' => function($value, $key, $index) {
                return ($value);
                },
                ))          
                ->add('Appliquer filtres', SubmitType::class)

                ->getForm();

Filter:

namespace loic\FilterBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Filter
 *
 * * @ORM\Entity(repositoryClass="loic\FilterBundle\Entity\FilterRepository")
 * @ORM\Table(name="filter", uniqueConstraints={@ORM\UniqueConstraint(name="idfilter_UNIQUE", columns={"idfilter"})}, indexes={@ORM\Index(name="fk_filter_filter_category1_idx", columns={"filter_category_idfilter_category"})})
 */
class Filter
{
    /**
     * @var integer
     *
     * @ORM\Column(name="idfilter", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $idfilter;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=45, nullable=true)
     */
    private $name;

    /**
     * @var \FilterCategory
     *
     * @ORM\ManyToOne(targetEntity="FilterCategory")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="filter_category_idfilter_category", referencedColumnName="idfilter_category")
     * })
     */
    private $filterCategoryfilterCategory;

    /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\ManyToMany(targetEntity="loic\ContentBundle\Entity\Content", mappedBy="filterfilter")
     */
    private $contentcontent;

    /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\ManyToMany(targetEntity="loic\UserBundle\Entity\User", mappedBy="filterfilter")
     */
    private $user;

    /**
     * @var string
     *
     * @ORM\Column(name="subtitle", type="string", length=45, nullable=true)
     */
    private $subtitle;

    /**
     * @var string
     *
     * @ORM\Column(name="description", type="string", length=45, nullable=true)
     */
    private $description;

    /**
     * @var string
     *
     * @ORM\Column(name="status", type="string", length=45, nullable=false)
     */
    private $status;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->contentcontent = new \Doctrine\Common\Collections\ArrayCollection();
        $this->user = new \Doctrine\Common\Collections\ArrayCollection();
        $this->status = 1;
    }

    /**
     *
     * @return the integer
     */
    public function getIdfilter() {
        return $this->idfilter;
    }

    /**
     *
     * @param
     *          $idfilter
     */
    public function setIdfilter($idfilter) {
        $this->idfilter = $idfilter;
        return $this;
    }

    /**
     *
     * @return the string
     */
    public function getName() {
        return $this->name;
    }

    /**
     *
     * @param
     *          $name
     */
    public function setName($name) {
        $this->name = $name;
        return $this;
    }

    /**
     *
     * @return the \FilterCategory
     */
    public function getFilterCategoryfilterCategory() {
        return $this->filterCategoryfilterCategory;
    }

    /**
     *
     * @param \FilterCategory $filterCategoryfilterCategory         
     */
    public function setFilterCategoryfilterCategory($filterCategoryfilterCategory) {
        $this->filterCategoryfilterCategory = $filterCategoryfilterCategory;
        return $this;
    }

    /**
     *
     * @return the \Doctrine\Common\Collections\Collection
     */
    public function getContentcontent() {
        return $this->contentcontent;
    }

    /**
     *
     * @param
     *          $contentcontent
     */
    public function setContentcontent($contentcontent) {
        $this->contentcontent = $contentcontent;
        return $this;
    }

    public function __toString(){

        return $this->name;
    }

    /**
     *
     * @return the \Doctrine\Common\Collections\Collection
     */
    public function getUser() {
        return $this->user;
    }

    /**
     *
     * @param
     *          $user
     */
    public function setUser($user) {
        $this->user = $user;
        return $this;
    }

    /**
     *
     * @return the string
     */
    public function getSubtitle() {
        return $this->subtitle;
    }

    /**
     *
     * @param
     *          $subtitle
     */
    public function setSubtitle($subtitle) {
        $this->subtitle = $subtitle;
        return $this;
    }

    /**
     *
     * @return the string
     */
    public function getDescription() {
        return $this->description;
    }

    /**
     *
     * @param
     *          $description
     */
    public function setDescription($description) {
        $this->description = $description;
        return $this;
    }

    /**
     *
     * @return the string
     */
    public function getStatus() {
        return $this->status;
    }

    /**
     *
     * @param
     *          $status
     */
    public function setStatus($status) {
        $this->status = $status;
        return $this;
    }



}
like image 719
L01C Avatar asked Sep 02 '25 13:09

L01C


1 Answers

You should switch your from field type to EntityType (extends ChoiceType).

There you can overwrite the way the choice_label property is generated.

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

$builder->add('category', EntityType::class, array(
    'class' => 'AppBundle:Category',
    'choice_label' => function ($category) {
        return $category->getDisplayName();
    }
));

Sorce: http://symfony.com/doc/current/reference/forms/types/entity.html

like image 159
Joe Avatar answered Sep 05 '25 17:09

Joe