Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Improve example context #1

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

Merged
merged 1 commit into from
Mar 2, 2017
Merged
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
111 changes: 98 additions & 13 deletions form/action_method.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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::

Expand All @@ -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:
Expand Down