From 0345790fe4b2fcf85d3dd04770a73b0cb53e3b84 Mon Sep 17 00:00:00 2001 From: Lewis Spears Date: Tue, 3 May 2016 10:20:37 +0100 Subject: [PATCH 1/4] Explain how to provide custom form options The documentation doesn't explain how to pass in form options, so an example was provided. --- book/forms.rst | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/book/forms.rst b/book/forms.rst index 0fde7985056..cb800f9d7ce 100644 --- a/book/forms.rst +++ b/book/forms.rst @@ -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 not longer pass in ``__construct()`` arguments. 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['usernameRequired'])); + $builder->add('password', PasswordType::class, array('required' => $options['passwordRequired'])); + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults(array( + 'username_required' => true, + 'password_required' => false, + )); + } + .. index:: single: Forms; Creating form classes From 0503805e5513a2f728dd08e173d123a961593c46 Mon Sep 17 00:00:00 2001 From: Lewis Spears Date: Tue, 3 May 2016 10:31:59 +0100 Subject: [PATCH 2/4] Fixed option name typo --- book/forms.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/book/forms.rst b/book/forms.rst index cb800f9d7ce..b767db38efa 100644 --- a/book/forms.rst +++ b/book/forms.rst @@ -1032,8 +1032,8 @@ to the ``form()`` or the ``form_start()`` helper: public function buildForm(FormBuilderInterface $builder, array $options) { - $builder->add('username', TextType::class, array('required' => $options['usernameRequired'])); - $builder->add('password', PasswordType::class, array('required' => $options['passwordRequired'])); + $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) From 24661b54486fafe57d5a0faa961af0bcf4dc4df2 Mon Sep 17 00:00:00 2001 From: Lewis Spears Date: Tue, 3 May 2016 10:35:49 +0100 Subject: [PATCH 3/4] Fixed indentation --- book/forms.rst | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/book/forms.rst b/book/forms.rst index b767db38efa..7f1acf7bffb 100644 --- a/book/forms.rst +++ b/book/forms.rst @@ -1032,16 +1032,16 @@ to the ``form()`` or the ``form_start()`` helper: 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'])); + $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, - )); + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults(array( + 'username_required' => true, + 'password_required' => false, + )); } .. index:: From 6268f35ecb89b50271fba9c0abbc1f29cccb1755 Mon Sep 17 00:00:00 2001 From: Lewis Spears Date: Tue, 3 May 2016 10:43:14 +0100 Subject: [PATCH 4/4] Altered the introduction to passing form options --- book/forms.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/book/forms.rst b/book/forms.rst index 7f1acf7bffb..a3c9dbe9961 100644 --- a/book/forms.rst +++ b/book/forms.rst @@ -1023,7 +1023,7 @@ to the ``form()`` or the ``form_start()`` helper: .. note:: - Now that the ``createForm()`` method requires the FQCN, you can not longer pass in ``__construct()`` arguments. To get + 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;