Y
Y
Yuri Matveenko2017-08-08 13:40:39
css
Yuri Matveenko, 2017-08-08 13:40:39

Shapes in Symphony, EntityType. Can you add your own option?

Comrades, here is the builder. Let's take social network as an example.

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('socialNetwork', EntityType::class, array(
                'class' => 'AppBundle:SocialNetwork',
                'choice_label' => 'name',
                'attr' => array(
                    'class' => 'select2 socialNetwork',
                    'style' => 'width: 200px'
                ),
                'empty_data' => 'John Doe',
            ))
            ->add('citys', EntityType::class, array(
                'class' => 'AppBundle:City',
                'choice_label' => function ($city) {
                    return $city->getName();
                },
                'attr' => array(
                    'class' => 'select2 city',
                    'style' => 'width: 200px',
                )
            ))/*
            ->add('price', TextType::class, array(
                'attr' => array(
                    'class' => 'price'
                )
            ))
            ->add('countFollowers', TextType::class, array(
                'attr' => array(
                    'class' => 'followers'
                )
            ))
            ->add('sex', ChoiceType::class, array(
                'choices'  => array(
                    'Мужчина' => true,
                    'Женщина' => false,
                ),
                'attr' => array(
                    'class' => 'select2',
                    'style' => 'width: 150px'
                )
            ))
            ->add('age', TextType::class, array(
                'attr' => array(
                    'class' => 'age'
                )
            ))*/
            ->add('save', SubmitType::class, array('label' => 'Применить'))
            ->getForm();
    }

It takes data from Entity:
<?php

namespace AppBundle\Entity;

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

/**
 * Class Tasks
 * @package AppBundle\Entity
 * @ORM\Table(name="social_network")
 * @ORM\Entity
 */
class SocialNetwork
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id_sn;


    /**
     * @ORM\Column(type="string");
     */
    protected $name;


    /**
     * @ORM\OneToMany(targetEntity="Tasks", mappedBy="social_network")
     */
    protected $tasks;


    /**
     * @ORM\OneToMany(targetEntity="Area", mappedBy="social_network")
     */
    protected $area;

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

    /**
     * Get idSn
     *
     * @return integer
     */
    public function getIdSn()
    {
        return $this->id_sn;
    }
..............

I need the input to have an option like "Not selected". How can I add it?
You can, of course, use ChoiseType and load into it all the fields with this "Not selected" in a loop, but this is a crutch.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
E
Eugene, 2019-03-22
@Slowfind

https://select2.org
selectric.js.org

O
Oleg, 2017-08-08
@letsrock_inita

Specify 'require' => false attribute in the form

B
BoShurik, 2017-08-08
@BoShurik

https://symfony.com/doc/current/reference/forms/ty...
Or https://symfony.com/doc/current/reference/forms/ty ... means "not selected")

E
Evgeny Svirsky, 2017-08-08
@e_svirsky

Instead of 'empty_data' => 'John Doe',
write empty_data => Not selected. This setting does just that.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question