Skip to content

Resolving $this usage outside a class on form.rst #7560

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 1 commit 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
113 changes: 72 additions & 41 deletions components/form.rst
Original file line number Diff line number Diff line change
Expand Up @@ -460,19 +460,29 @@ builder:

.. code-block:: php-symfony

// src/Acme/TaskBundle/Controller/DefaultController.php
namespace Acme\TaskBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\DateType;

// ...
class DefaultController extends Controller
{
public function newAction(Request $request)
{
$defaults = array(
'dueDate' => new \DateTime('tomorrow'),
);

$defaults = array(
'dueDate' => new \DateTime('tomorrow'),
);
$form = $this->createFormBuilder($defaults)
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this documents the component maybe $this-> should be replaced by FormFactory:: with the proper use statement instead of the current proposal. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah yes forgot about my comment, this is a good fix!

->add('task', TextType::class)
->add('dueDate', DateType::class)
->getForm();

$form = $this->createFormBuilder($defaults)
->add('task', TextType::class)
->add('dueDate', DateType::class)
->getForm();
// ...
}
}

.. tip::

Expand Down Expand Up @@ -532,18 +542,23 @@ by ``handleRequest()`` to determine whether a form has been submitted):

.. code-block:: php-symfony

use Symfony\Component\Form\Extension\Core\Type\FormType;
// src/Acme/TaskBundle/Controller/DefaultController.php
namespace Acme\TaskBundle\Controller;

// ...
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\Extension\Core\Type\FormType;

public function searchAction()
class DefaultController extends Controller
{
$formBuilder = $this->createFormBuilder(null, array(
'action' => '/search',
'method' => 'GET',
));
public function searchAction()
{
$formBuilder = $this->createFormBuilder(null, array(
'action' => '/search',
'method' => 'GET',
));

// ...
// ...
}
}

.. _component-form-intro-handling-submission:
Expand Down Expand Up @@ -589,29 +604,34 @@ method:

.. code-block:: php-symfony

// src/Acme/TaskBundle/Controller/DefaultController.php
namespace Acme\TaskBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\DateType;

// ...

public function newAction(Request $request)
class DefaultController extends Controller
{
$form = $this->createFormBuilder()
->add('task', TextType::class)
->add('dueDate', DateType::class)
->getForm();
public function newAction(Request $request)
{
$form = $this->createFormBuilder()
->add('task', TextType::class)
->add('dueDate', DateType::class)
->getForm();

$form->handleRequest($request);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();

// ... perform some action, such as saving the data to the database
// ... perform some action, such as saving the data to the database

return $this->redirectToRoute('task_success');
}
return $this->redirectToRoute('task_success');
}

// ...
// ...
}
}

This defines a common form "workflow", which contains 3 different possibilities:
Expand Down Expand Up @@ -660,22 +680,33 @@ option when building each field:

.. code-block:: php-symfony

// src/Acme/TaskBundle/Controller/DefaultController.php
namespace Acme\TaskBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Type;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\DateType;

$form = $this->createFormBuilder()
->add('task', TextType::class, array(
'constraints' => new NotBlank(),
))
->add('dueDate', DateType::class, array(
'constraints' => array(
new NotBlank(),
new Type(\DateTime::class),
)
))
->getForm();
class DefaultController extends Controller
{
public function newAction(Request $request)
{
$form = $this->createFormBuilder()
->add('task', TextType::class, array(
'constraints' => new NotBlank(),
))
->add('dueDate', DateType::class, array(
'constraints' => array(
new NotBlank(),
new Type(\DateTime::class),
)
))
->getForm();
// ...
}
}

When the form is bound, these validation constraints will be applied automatically
and the errors will display next to the fields on error.
Expand Down