Skip to content

Simplifying the code block #13446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions form/form_collections.rst
Original file line number Diff line number Diff line change
Expand Up @@ -251,23 +251,21 @@ The first thing you need to do is to let the form collection know that it will
receive an unknown number of tags. So far you've added two tags and the form
type expects to receive exactly two, otherwise an error will be thrown:
``This form should not contain extra fields``. To make this flexible,
add the ``allow_add`` option to your collection field::
add the ``allow_add`` option to your collection field:

// src/Form/TaskType.php
```php
// src/Form/TaskType.php

public function buildForm(FormBuilderInterface $builder, array $options)
{
// ...
use Symfony\Component\Form\FormBuilderInterface;

public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('description');

$builder->add('tags', CollectionType::class, [
'entry_type' => TagType::class,
'entry_options' => ['label' => false],
'allow_add' => true,
]);
}
$builder->add('tags', CollectionType::class, [
'entry_type' => TagType::class,
'entry_options' => ['label' => false],
'allow_add' => true,
]);
}
```

In addition to telling the field to accept any number of submitted objects, the
``allow_add`` also makes a *"prototype"* variable available to you. This "prototype"
Expand Down