diff --git a/form/action_method.rst b/form/action_method.rst index 64098cfc647..3641b45fc4b 100644 --- a/form/action_method.rst +++ b/form/action_method.rst @@ -9,15 +9,63 @@ 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()``: - $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:: @@ -25,15 +73,52 @@ 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:: + + .. 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() + { + // ... - use AppBundle\Form\TaskType; - // ... + $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: