Skip to content

Improve example context #7563

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
wants to merge 3 commits into from
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
115 changes: 100 additions & 15 deletions form/action_method.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,116 @@ 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()
{

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This blank line is useless.

// ...

$form = $this->createFormBuilder($task)
->setAction($this->generateUrl('target_route'))
->setMethod('GET')
->add('task', TextType::class)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR should target 2.7 and use type names (i.e 'text').

->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::

This example assumes that you've created a route called ``target_route``
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:
Expand Down