Skip to content

Added a new section to explain how to pass options to forms #12278

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

Merged
merged 1 commit into from
Sep 6, 2019
Merged
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
76 changes: 76 additions & 0 deletions forms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,82 @@ powerful feature.
Other Common Form Features
--------------------------

Passing Options to Forms
~~~~~~~~~~~~~~~~~~~~~~~~

If you :ref:`create forms in classes <creating-forms-in-classes>`, when building
the form in the controller you can pass custom options to it as the third optional
argument of ``createForm()``::

// src/Controller/TaskController.php
use App\Form\Type\TaskType;
// ...

class TaskController extends AbstractController
{
public function new()
{
$task = new Task();
// use some PHP logic to decide if this form field is required or not
$dueDateIsRequired = ...

$form = $this->createForm(TaskType::class, $task, [
'require_due_date' => $dueDateIsRequired,
]);

// ...
}
}

If you try to use the form now, you'll see an error message: *The option
"require_due_date" does not exist.* That's because forms must declare all the
options they accept using the ``configureOptions()`` method::

// src/Form/Type/TaskType.php
use Symfony\Component\OptionsResolver\OptionsResolver;
// ...

class TaskType extends AbstractType
{
// ...

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
// ...,
'require_due_date' => false,
]);

// you can also define the allowed types, allowed values and
// any other feature supported by the OptionsResolver component
$resolver->setAllowedTypes('require_due_date', 'bool');
}
}

Now you can use this new form option inside the ``buildForm()`` method::

// src/Form/Type/TaskType.php
namespace App\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\FormBuilderInterface;

class TaskType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
// ...
->add('dueDate', DateType::class, [
'required' => $options['require_due_date'],
])
;
}

// ...
}

Form Type Options
~~~~~~~~~~~~~~~~~

Expand Down