New in Symfony 6.1: Customizable Collection Prototypes

Contributed by Michael Käfer in #45605.

The Symfony Form component is so stable and provides so many features, that we rarely add new features to it. However, in Symfony 6.1 we're improving its developer experience with a new feature to customize collection prototypes. The CollectionType form field is used to render a collection of other fields or forms. When the field allows to add new elements to the collection, it defines a prototype option. This contains the HTML code needed to render the new empty collection item (you've probably seen it as the data-prototype HTML attribute of the element that wraps the entire collection). The only drawback is that collection prototypes are not entirely configurable. You can set their initial values with the prototype-data option, but that's pretty much it. That's why in Symfony 6.1 we're adding a new option called prototype_options so you can configure collection prototypes. The options defined in prototype_options are passed to the form type specified in the entry_type option when creating its prototype. In practice, this allows to have different options depending on whether you are adding a new entry or editing an existing entry:

    $builder->add('names', CollectionType::class, [
'entry_type'   => TextType::class,

// this is used when editing items in the collection
'entry_options'  => [
    'attr' => ['class' => 'item-edit'],
    'help'  => 'You cannot edit existing names.',
    'disabled' => true,
],

// this is used when adding new items to the collection
'prototype_options'  => [
    'attr' => ['class' => 'item-add'],
    'help'  => 'Check out the rules to create new names',
    'help_html' => true,
],

]);

                Sponsor the Symfony project.

https://symfony.com/blog/new-in-symfony-6-1-customizable-collection-prototypes?utm_source=Symfony%20Blog%20Feed&utm_medium=feed

Creată 3y | 9 mai 2022, 08:20:08


Autentifică-te pentru a adăuga comentarii

Alte posturi din acest grup

A Week of Symfony #962 (June 2–8, 2025)

This week, development activity focused on the upcoming Symfony 7.4 and 8.0 versions, which will deprecate and remove many features. In addition, we published a case study about Yousign. Finally, we'r

8 iun. 2025, 07:50:05 | Symfony
Case study - Yousign: Scaling Trust with Smart, Scalable Architecture

As digital signatures become the norm in modern business, Yousign has established itself as a trusted leader across Europe. Behind its simple, intuitive interface is a powerful technical engine, handl

6 iun. 2025, 07:10:24 | Symfony
SymfonyOnline June 2025 starts next week!

Get ready for the exciting SymfonyOnline June 2025, kicking off in a few days only! There’s still time to register and join the international online Symfony conference—along with pre-conferenc

5 iun. 2025, 10:20:09 | Symfony
A Week of Symfony #961 (May 26 – June 1, 2025)

This week, Symfony released the stable version of Symfony 7.3, which includes lots of amazing new features. We also published the maintenance versions 6.4.22 and 7.2.7.

Symfony development highlights

1 iun. 2025, 08:50:16 | Symfony
New in Symfony 7.3: DX Improvements (part 2)

This is the second part of the blog post showcasing the main DX (developer experience) features introduced in Symfony 7.3. Read the first part of this blog post.

Verify URI Signatures… https://symfon

29 mai 2025, 09:10:19 | Symfony
Symfony 6.4.22 released

Symfony 6.4.22 has just been released. Read the Symfony upgrade guide to learn more about upgrading Symfony and use the SymfonyInsight upgrade reports to detect the code you will need to change in you

29 mai 2025, 09:10:19 | Symfony
Symfony 7.2.7 released

Symfony 7.2.7 has just been released. Read the Symfony upgrade guide to learn more about upgrading Symfony and use the SymfonyInsight upgrade reports to detect the code you will need to change in your

29 mai 2025, 09:10:18 | Symfony