Symfony EntityType - How to use custom layout?

Krzysztof Sowa

In my Symfony 3.x project, I have 3 entities:

  1. Project
  2. Property
  3. Category

Assumptions:

  • Each Project has multiple Properties.
  • Each Property has one Category.
  • Each Property might have parent Property.

I would like to render Symfony form for Project entity. I'm using EntityType field for properties. However, instead of displaying them in one, long list, I would like to divide them in columns, with Categories as headers.

Regular way of displaying EntityType field:

enter image description here

What I would like to get:

enter image description here

How do I do that? - Without using dirty hacks in entities or views.

Krzysztof Sowa

So the only way I found it working was:

In Repository class (pulling list of all properties with child properties and categories):

$this->getEntityManager()
    ->createQueryBuilder()
    ->select('t, category, children')
    ->join('t.category', 'category')
    ->leftJoin('t.children', 'children')
    ->where('t.parent IS NULL')
    ->orderBy('category.sortOrder', 'ASC')
    ->addOrderBy('t.sortOrder', 'ASC')
    ->addOrderBy('t.name', 'ASC');

$entities = $query->getResult();

$options = [];
/** @var Property $entity */
foreach ($entities as $entity) {
    $options[$entity->getCategory()->getName()][] = $entity;
}

In Entity class (pulling the list of IDs of selected properties, to preselect checkboxes in the view file):

public function getPropertyIds() {
    $properties = $this->getProperties();

    $propertyIds = [];
    foreach ($properties as $property) {
        $propertyIds[] = $property->getId();
    }

    return $propertyIds;
}

Edition form class, so the data can be validated:

$builder
    ->add(
        'properties',
        EntityType::class,
        [
            'label' => 'Properties',
            'class' => Property::class,
            'choice_label' => 'name',
            'placeholder' => '',
            'expanded' => true,
            'multiple' => true,
            'required' => false,
        ]
    );

And finally, the view file:

{% for categoryName, items in properties %}
    <h2>{{ categoryName }}</h2>

    <ul>
        {% for property in items %}  
            <li>
                <input type="checkbox"
                       name="{{ form.properties.vars.full_name }}[]"
                       value="{{ property.id }}"
                       id="{{ form.properties.vars.id }}_{{ property.id }}">
                <label for="{{ form.properties.vars.id }}_{{ property.id }}">
                    {{ property.name }}
                </label>
            </li>
        {% endfor %}
    </ul>
{% endfor %}

{% do form.properties.setRendered %}

(I omitted the "checked" and "children" part in the view)

However this solution is not ideal in my point of view. I would rather to get rid of manually generating <input...> in the view - I would rather want to use some helper functions.

Anyway, this is some kind of low-level solution to my problem. Hope that helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Symfony Form EntityType, add custom data to choice

From Dev

How to use Android Layout and custom GUI design in this

From Dev

How to select custom layout to use in Apache POI

From Dev

How to use a base layout and custom it in android

From Dev

How to use Android Layout and custom GUI design in this

From Dev

Android - How to layout and use a custom View?

From Dev

How to use a base layout and custom it in android

From Dev

Symfony EntityType as autocomplete

From Dev

Symfony Form Validation EntityType

From Dev

How to use custom css file for layout of a module in Yii2

From Dev

Symfony2: How to use constraints on custom compound form type?

From Dev

How to create and use a form in a custom class in Symfony 2

From Dev

How to use my custom Symfony 3.0 Library/Bundle/HowIsCalled

From Dev

Symfony2: How to use constraints on custom compound form type?

From Dev

Symfony form EntityType without preloading

From Dev

Symfony form EntityType without preloading

From Dev

Symfony 3 - Pass Object to EntityType

From Dev

How do I use a custom keyboard layout created with Microsoft Keyboard Layout Creator in Windows 8?

From Dev

how to show custom layout in my main layout?

From Dev

How to dim the background of a custom layout?

From Dev

How to make a custom keyboard layout?

From Dev

How to make a custom keyboard layout?

From Dev

Symfony 2.7 choice_attr with EntityType field

From Dev

Symfony2 Could not load type EntityType

From Dev

Symfony form builder default select by EntityType

From Dev

Define default selection if no data in db - Symfony EntityType

From Dev

Symfony - EntityType select repeats the same option

From Dev

Symfony: Form EntityType loading from another database

From Dev

Save EntityType choice as string - Symfony 4

Related Related

  1. 1

    Symfony Form EntityType, add custom data to choice

  2. 2

    How to use Android Layout and custom GUI design in this

  3. 3

    How to select custom layout to use in Apache POI

  4. 4

    How to use a base layout and custom it in android

  5. 5

    How to use Android Layout and custom GUI design in this

  6. 6

    Android - How to layout and use a custom View?

  7. 7

    How to use a base layout and custom it in android

  8. 8

    Symfony EntityType as autocomplete

  9. 9

    Symfony Form Validation EntityType

  10. 10

    How to use custom css file for layout of a module in Yii2

  11. 11

    Symfony2: How to use constraints on custom compound form type?

  12. 12

    How to create and use a form in a custom class in Symfony 2

  13. 13

    How to use my custom Symfony 3.0 Library/Bundle/HowIsCalled

  14. 14

    Symfony2: How to use constraints on custom compound form type?

  15. 15

    Symfony form EntityType without preloading

  16. 16

    Symfony form EntityType without preloading

  17. 17

    Symfony 3 - Pass Object to EntityType

  18. 18

    How do I use a custom keyboard layout created with Microsoft Keyboard Layout Creator in Windows 8?

  19. 19

    how to show custom layout in my main layout?

  20. 20

    How to dim the background of a custom layout?

  21. 21

    How to make a custom keyboard layout?

  22. 22

    How to make a custom keyboard layout?

  23. 23

    Symfony 2.7 choice_attr with EntityType field

  24. 24

    Symfony2 Could not load type EntityType

  25. 25

    Symfony form builder default select by EntityType

  26. 26

    Define default selection if no data in db - Symfony EntityType

  27. 27

    Symfony - EntityType select repeats the same option

  28. 28

    Symfony: Form EntityType loading from another database

  29. 29

    Save EntityType choice as string - Symfony 4

HotTag

Archive