8000 minor #7560 Resolving $this usage outside a class on form.rst (matthi… · ScreamZ/symfony-docs@a4eeb9e · GitHub
[go: up one dir, main page]

Skip to content

Commit a4eeb9e

Browse files
committed
minor symfony#7560 Resolving $this usage outside a class on form.rst (matthieu88160)
This PR was submitted for the 3.2 branch but it was merged into the 2.7 branch instead (closes symfony#7560). Discussion ---------- Resolving $this usage outside a class on form.rst This commit take care of the framework use code examples only. The "Creating a simple Form" define the coding place to be into a controller action method. Unlikely, the 8000 documentation consider into the other examples that the reader will know the place of the sample code. This does not allow the examples to be standalone and can lead some disappointment for which one want to have informations about a single section. This commit offer to place the code samples into the default controller to create a logic use of the "$this" special variable. Commits ------- 8cf9cd2 Resolving $this usage outside a class on form.rst
2 parents 73ffb51 + 8cf9cd2 commit a4eeb9e

File tree

1 file changed

+79
-38
lines changed

1 file changed

+79
-38
lines changed

components/form.rst

Lines changed: 79 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -444,14 +444,29 @@ builder:
444444

445445
.. code-block:: php-symfony
446446

447-
$defaults = array(
448-
'dueDate' => new \DateTime('tomorrow'),
449-
);
447+
// src/Acme/TaskBundle/Controller/DefaultController.php
448+
namespace Acme\TaskBundle\Controller;
450449

451-
$form = $this->createFormBuilder($defaults)
452-
->add('task', 'text')
453-
->add('dueDate', 'date')
454-
->getForm();
450+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
451+
use Symfony\Component\Form\Extension\Core\Type\TextType;
452+
use Symfony\Component\Form\Extension\Core\Type\DateType;
453+
454+
class DefaultController extends Controller
455+
{
456+
public function newAction(Request $request)
457+
{
458+
$defaults = array(
459+
'dueDate' => new \DateTime('tomorrow'),
460+
);
461+
462+
$form = $this->createFormBuilder($defaults)
463+
->add('task', 'text')
464+
->add('dueDate', 'date')
465+
->getForm();
466+
467+
// ...
468+
}
469+
}
455470

456471
.. tip::
457472

@@ -511,16 +526,23 @@ by ``handleRequest()`` to determine whether a form has been submitted):
511526

512527
.. code-block:: php-symfony
513528

514-
// ...
529+
// src/Acme/TaskBundle/Controller/DefaultController.php
530+
namespace Acme\TaskBundle\Controller;
531+
532+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
533+
use Symfony\Component\Form\Extension\Core\Type\FormType;
515534

516-
public function searchAction()
535+
class DefaultController extends Controller
517536
{
518-
$formBuilder = $this->createFormBuilder(null, array(
519-
'action' => '/search',
520-
'method' => 'GET',
521-
));
537+
public function searchAction()
538+
{
539+
$formBuilder = $this->createFormBuilder(null, array(
540+
'action' => '/search',
541+
'method' => 'GET',
542+
));
522543

523-
// ...
544+
// ...
545+
}
524546
}
525547

526548
.. _component-form-intro-handling-submission:
@@ -562,26 +584,34 @@ method:
562584

563585
.. code-block:: php-symfony
564586

565-
// ...
587+
// src/Acme/TaskBundle/Controller/DefaultController.php
588+
namespace Acme\TaskBundle\Controller;
566589

567-
public function newAction(Request $request)
590+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
591+
use Symfony\Component\Form\Extension\Core\Type\TextType;
592+
use Symfony\Component\Form\Extension\Core\Type\DateType;
593+
594+
class DefaultController extends Controller
568595
{
569-
$form = $this->createFormBuilder()
570-
->add('task', 'text')
571-
->add('dueDate', 'date')
572-
->getForm();
596+
public function newAction(Request $request)
597+
{
598+
$form = $this->createFormBuilder()
599+
->add('task', 'text')
600+
->add('dueDate', 'date')
601+
->getForm();
573602

574-
$form->handleRequest($request);
603+
$form->handleRequest($request);
575604

576-
if ($form->isSubmitted() && $form->isValid()) {
577-
$data = $form->getData();
605+
if ($form->isSubmitted() && $form->isValid()) {
606+
$data = $form->getData();
578607

579-
// ... perform some action, such as saving the data to the database
608+
// ... perform some action, such as saving the data to the database
580609

581-
return $this->redirectToRoute('task_success');
582-
}
610+
return $this->redirectToRoute('task_success');
611+
}
583612

584-
// ...
613+
// ...
614+
}
585615
}
586616

587617
This defines a common form "workflow", which contains 3 different possibilities:
@@ -628,20 +658,31 @@ option when building each field:
628658

629659
.. code-block:: php-symfony
630660

661+
// src/Acme/TaskBundle/Controller/DefaultController.php
662+
namespace Acme\TaskBundle\Controller;
663+
664+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
631665
use Symfony\Component\Validator\Constraints\NotBlank;
632666
use Symfony\Component\Validator\Constraints\Type;
633667

634-
$form = $this->createFormBuilder()
635-
->add('task', 'text', array(
636-
'constraints' => new NotBlank(),
637-
))
638-
->add('dueDate', 'date', array(
639-
'constraints' => array(
640-
new NotBlank(),
641-
new Type(\DateTime::class),
642-
)
643-
))
644-
->getForm();
668+
class DefaultController extends Controller
669+
{
670+
public function newAction(Request $request)
671+
{
672+
$form = $this->createFormBuilder()
673+
->add('task', 'text', array(
674+
'constraints' => new NotBlank(),
675+
))
676+
->add('dueDate', 'date', array(
677+
'constraints' => array(
678+
new NotBlank(),
679+
new Type(\DateTime::class),
680+
)
681+
))
682+
->getForm();
683+
// ...
684+
}
685+
}
645686

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

0 commit comments

Comments
 (0)
0