I
I
Ivan Antonov2015-08-03 13:52:52
symfony
Ivan Antonov, 2015-08-03 13:52:52

How to group select items in Symfony form builder?

You need to make a select by the districts of Nizhny Novgorod and the region, dividing them into groups, like this:

<select id="offer_add_district" name="offer_add[district]">
        <optgroup label="Нижний Новгород">
                <option value="1">Автозаводский</option>
                <option value="2">Канавинский</option>
                <option value="3">Ленинский</option>
                <option value="4">Московский</option>
                <option value="5">Нижегородский</option>
       </optgroup>
       <optgroup label="Нижегородская обл.">
                <option value="9">Ардатовский</option>
                <option value="10">Арзамасский</option>
                <option value="11">Балахнинский</option>
                <option value="12">Богородский</option>
       </optgroup>
</select>

I would do like this:
$builder->add('district', null, ['group_by' => 'center']);

But center is a flag that indicates whether the area belongs to a city(1) or an area(0).
There is an option to create a table with cities and regions and in the center column set the relationship between the city and the region.
But the site will only have Nizhny Novgorod and the region, I would not want to create extra tables in the database.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2015-08-04
@antonowano

// Entity
class District
{
    /**
     * @ORM\Colimn...
     */
    private $center;

    /**
     * @var string
     */
    private $groupName;

    public function getGroupName()
    {
        return $this->getCenter() ? 'Center' : 'Region';
    }
}

// ... 
$builder->add('district', null, ['group_by' => 'groupName']);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question