From d1afeead5b663b1323d34c162382a1c05e603bd2 Mon Sep 17 00:00:00 2001 From: matthieu88160 Date: Thu, 2 Mar 2017 09:13:34 +0100 Subject: [PATCH 1/2] Improve example context This commit add the component standalone examples and place the existing ones into a controller context. --- form/action_method.rst | 111 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 98 insertions(+), 13 deletions(-) diff --git a/form/action_method.rst b/form/action_method.rst index 64098cfc647..41c89d5a843 100644 --- a/form/action_method.rst +++ b/form/action_method.rst @@ -11,13 +11,61 @@ parameters. You can do so in a few different ways. If you use the :class:`Symfony\\Component\\Form\\FormBuilder` to build your form, you can use ``setAction()`` and ``setMethod()``:: - $form = $this->createFormBuilder($task) - ->setAction($this->generateUrl('target_route')) - ->setMethod('GET') - ->add('task', TextType::class) - ->add('dueDate', DateType::class) - ->add('save', SubmitType::class) - ->getForm(); +.. configuration-block:: + + .. code-block:: php-symfony + + // AppBundle/Controller/DefaultController.php + namespace AppBundle\Controller; + + use Symfony\Bundle\FrameworkBundle\Controller\Controller; + use Symfony\Component\Form\Extension\Core\Type\TextType; + use Symfony\Component\Form\Extension\Core\Type\DateType; + use Symfony\Component\Form\Extension\Core\Type\SubmitType; + + class DefaultController extends Controller + { + public function newAction() + { + + // ... + + $form = $this->createFormBuilder($task) + ->setAction($this->generateUrl('target_route')) + ->setMethod('GET') + ->add('task', TextType::class) + ->add('dueDate', DateType::class) + ->add('save', SubmitType::class) + ->getForm(); + + // ... + } + } + + + .. code-block:: php-standalone + + use Symfony\Component\Form\Forms; + use Symfony\Component\Form\Extension\Core\Type\FormType; + use Symfony\Component\Form\Extension\Core\Type\TextType; + use Symfony\Component\Form\Extension\Core\Type\DateType; + use Symfony\Component\Form\Extension\Core\Type\SubmitType; + + // ... + + $formFactoryBuilder = Forms::createFormFactoryBuilder(); + + // Form factory builder configuration ... + + $formFactory = $formFactoryBuilder->getFormFactory(); + + $form = $formFactory->createBuilder(FormType::class, $task) + ->setAction($this->generateUrl('target_route')) + ->setMethod('GET') + ->add('task', TextType::class) + ->add('dueDate', DateType::class) + ->add('save', SubmitType::class) + ->getForm(); .. note:: @@ -27,13 +75,50 @@ form, you can use ``setAction()`` and ``setMethod()``:: When using a form type class, you can pass the action and method as form options:: - use AppBundle\Form\TaskType; - // ... +.. configuration-block:: + + .. code-block:: php-symfony + + // AppBundle/Controller/DefaultController.php + namespace AppBundle\Controller; + + use Symfony\Bundle\FrameworkBundle\Controller\Controller; + use AppBundle\Form\TaskType; + + class DefaultController extends Controller + { + public function newAction() + { + // ... + + $form = $this->createForm(TaskType::class, $task, array( + 'action' => $this->generateUrl('target_route'), + 'method' => 'GET', + )); - $form = $this->createForm(TaskType::class, $task, array( - 'action' => $this->generateUrl('target_route'), - 'method' => 'GET', - )); + // ... + } + } + + + .. code-block:: php-standalone + + use Symfony\Component\Form\Forms; + use Symfony\Component\Form\Extension\Core\Type\TextType; + use Symfony\Component\Form\Extension\Core\Type\DateType; + use Symfony\Component\Form\Extension\Core\Type\SubmitType; + use AppBundle\Form\TaskType; + + $formFactoryBuilder = Forms::createFormFactoryBuilder(); + + // Form factory builder configuration ... + + $formFactory = $formFactoryBuilder->getFormFactory(); + + $form = $formFactory->create(TaskType::class, $task, array( + 'action' => $this->generateUrl('target_route'), + 'method' => 'GET', + )); Finally, you can override the action and method in the template by passing them to the ``form()`` or the ``form_start()`` helper functions: From 38ac0b3ef4935e46f3a06a072dbbe12ecfe1401c Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Wed, 15 Mar 2017 12:44:53 +0100 Subject: [PATCH 2/2] Fixed the RST syntax --- form/action_method.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/form/action_method.rst b/form/action_method.rst index 41c89d5a843..3641b45fc4b 100644 --- a/form/action_method.rst +++ b/form/action_method.rst @@ -9,7 +9,7 @@ URL under which the form was rendered. Sometimes you want to change these parameters. You can do so in a few different ways. If you use the :class:`Symfony\\Component\\Form\\FormBuilder` to build your -form, you can use ``setAction()`` and ``setMethod()``:: +form, you can use ``setAction()`` and ``setMethod()``: .. configuration-block:: @@ -73,7 +73,7 @@ form, you can use ``setAction()`` and ``setMethod()``:: that points to the controller that processes the form. When using a form type class, you can pass the action and method as form -options:: +options: .. configuration-block::