Skip to content

Explain how to provide custom form options #6527

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
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
23 changes: 23 additions & 0 deletions book/forms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,29 @@ to the ``form()`` or the ``form_start()`` helper:
a PUT, PATCH or DELETE request. Read the cookbook chapter
":doc:`/cookbook/routing/method_parameters`" for more information.

.. note::

Now that the ``createForm()`` method requires the FQCN, you can no longer pass in *dynamic* ``__construct()`` arguments. This is not to be confused with :ref:`form-as-services`. To get
around this, you will need to create the default options in the form class ``configureOptions()`` method::

use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\TextType;

public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('username', TextType::class, array('required' => $options['username_required']));
$builder->add('password', PasswordType::class, array('required' => $options['password_required']));
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'username_required' => true,
'password_required' => false,
));
}

.. index::
single: Forms; Creating form classes

Expand Down