@@ -387,22 +387,6 @@ is created from the form factory.
387387
388388.. configuration-block ::
389389
390- .. code-block :: php-standalone
391-
392- use Symfony\Component\Form\Extension\Core\Type\DateType;
393- use Symfony\Component\Form\Extension\Core\Type\TextType;
394-
395- // ...
396-
397- $form = $formFactory->createBuilder()
398- ->add('task', TextType::class)
399- ->add('dueDate', DateType::class)
400- ->getForm();
401-
402- var_dump($twig->render('new.html.twig', [
403- 'form' => $form->createView(),
404- ]));
405-
406390 .. code-block :: php-symfony
407391
408392 // src/Controller/TaskController.php
@@ -431,6 +415,22 @@ is created from the form factory.
431415 }
432416 }
433417
418+ .. code-block :: php-standalone
419+
420+ use Symfony\Component\Form\Extension\Core\Type\DateType;
421+ use Symfony\Component\Form\Extension\Core\Type\TextType;
422+
423+ // ...
424+
425+ $form = $formFactory->createBuilder()
426+ ->add('task', TextType::class)
427+ ->add('dueDate', DateType::class)
428+ ->getForm();
429+
430+ var_dump($twig->render('new.html.twig', [
431+ 'form' => $form->createView(),
432+ ]));
433+
434434 As you can see, creating a form is like writing a recipe: you call ``add() ``
435435for each new field you want to create. The first argument to ``add() `` is the
436436name of your field, and the second is the fully qualified class name. The Form
@@ -447,23 +447,6 @@ an "edit" form), pass in the default data when creating your form builder:
447447
448448.. configuration-block ::
449449
450- .. code-block :: php-standalone
451-
452- use Symfony\Component\Form\Extension\Core\Type\DateType;
453- use Symfony\Component\Form\Extension\Core\Type\FormType;
454- use Symfony\Component\Form\Extension\Core\Type\TextType;
455-
456- // ...
457-
458- $defaults = [
459- 'dueDate' => new \DateTime('tomorrow'),
460- ];
461-
462- $form = $formFactory->createBuilder(FormType::class, $defaults)
463- ->add('task', TextType::class)
464- ->add('dueDate', DateType::class)
465- ->getForm();
466-
467450 .. code-block :: php-symfony
468451
469452 // src/Controller/DefaultController.php
@@ -490,6 +473,23 @@ an "edit" form), pass in the default data when creating your form builder:
490473 }
491474 }
492475
476+ .. code-block :: php-standalone
477+
478+ use Symfony\Component\Form\Extension\Core\Type\DateType;
479+ use Symfony\Component\Form\Extension\Core\Type\FormType;
480+ use Symfony\Component\Form\Extension\Core\Type\TextType;
481+
482+ // ...
483+
484+ $defaults = [
485+ 'dueDate' => new \DateTime('tomorrow'),
486+ ];
487+
488+ $form = $formFactory->createBuilder(FormType::class, $defaults)
489+ ->add('task', TextType::class)
490+ ->add('dueDate', DateType::class)
491+ ->getForm();
492+
493493 .. tip ::
494494
495495 In this example, the default data is an array. Later, when you use the
@@ -533,19 +533,6 @@ by :method:`Symfony\\Component\\Form\\Form::handleRequest` to determine whether
533533
534534.. configuration-block ::
535535
536- .. code-block :: php-standalone
537-
538- use Symfony\Component\Form\Extension\Core\Type\FormType;
539-
540- // ...
541-
542- $formBuilder = $formFactory->createBuilder(FormType::class, null, [
543- 'action' => '/search',
544- 'method' => 'GET',
545- ]);
546-
547- // ...
548-
549536 .. code-block :: php-symfony
550537
551538 // src/Controller/DefaultController.php
@@ -567,46 +554,28 @@ by :method:`Symfony\\Component\\Form\\Form::handleRequest` to determine whether
567554 }
568555 }
569556
570- .. _component-form-intro-handling-submission :
571-
572- Handling Form Submissions
573- ~~~~~~~~~~~~~~~~~~~~~~~~~
574-
575- To handle form submissions, use the :method: `Symfony\\ Component\\ Form\\ Form::handleRequest `
576- method:
577-
578- .. configuration-block ::
579-
580557 .. code-block :: php-standalone
581558
582- use Symfony\Component\Form\Extension\Core\Type\DateType;
583- use Symfony\Component\Form\Extension\Core\Type\TextType;
584- use Symfony\Component\HttpFoundation\RedirectResponse;
585- use Symfony\Component\HttpFoundation\Request;
559+ use Symfony\Component\Form\Extension\Core\Type\FormType;
586560
587561 // ...
588562
589- $form = $formFactory->createBuilder()
590- ->add('task', TextType::class)
591- ->add('dueDate', DateType::class)
592- ->getForm();
593-
594- $request = Request::createFromGlobals();
595-
596- $form->handleRequest($request);
563+ $formBuilder = $formFactory->createBuilder(FormType::class, null, [
564+ 'action' => '/search',
565+ 'method' => 'GET',
566+ ]);
597567
598- if ($form->isSubmitted() && $form->isValid()) {
599- $data = $form->getData();
568+ // ...
600569
601- // ... perform some action, such as saving the data to the database
570+ .. _ component-form-intro-handling-submission :
602571
603- $response = new RedirectResponse('/task/success');
604- $response->prepare($request);
572+ Handling Form Submissions
573+ ~~~~~~~~~~~~~~~~~~~~~~~~~
605574
606- return $response->send();
607- }
575+ To handle form submissions, use the :method: ` Symfony \\ Component \\ Form \\ Form::handleRequest `
576+ method:
608577
609- // ...
578+ .. configuration-block ::
610579
611580 .. code-block :: php-symfony
612581
@@ -640,6 +609,37 @@ method:
640609 }
641610 }
642611
612+ .. code-block :: php-standalone
613+
614+ use Symfony\Component\Form\Extension\Core\Type\DateType;
615+ use Symfony\Component\Form\Extension\Core\Type\TextType;
616+ use Symfony\Component\HttpFoundation\RedirectResponse;
617+ use Symfony\Component\HttpFoundation\Request;
618+
619+ // ...
620+
621+ $form = $formFactory->createBuilder()
622+ ->add('task', TextType::class)
623+ ->add('dueDate', DateType::class)
624+ ->getForm();
625+
626+ $request = Request::createFromGlobals();
627+
628+ $form->handleRequest($request);
629+
630+ if ($form->isSubmitted() && $form->isValid()) {
631+ $data = $form->getData();
632+
633+ // ... perform some action, such as saving the data to the database
634+
635+ $response = new RedirectResponse('/task/success');
636+ $response->prepare($request);
637+
638+ return $response->send();
639+ }
640+
641+ // ...
642+
643643 .. caution ::
644644
645645 The form's ``createView() `` method should be called *after * ``handleRequest() `` is
@@ -672,25 +672,6 @@ option when building each field:
672672
673673.. configuration-block ::
674674
675- .. code-block :: php-standalone
676-
677- use Symfony\Component\Form\Extension\Core\Type\DateType;
678- use Symfony\Component\Form\Extension\Core\Type\TextType;
679- use Symfony\Component\Validator\Constraints\NotBlank;
680- use Symfony\Component\Validator\Constraints\Type;
681-
682- $form = $formFactory->createBuilder()
683- ->add('task', TextType::class, [
684- 'constraints' => new NotBlank(),
685- ])
686- ->add('dueDate', DateType::class, [
687- 'constraints' => [
688- new NotBlank(),
689- new Type(\DateTime::class),
690- ],
691- ])
692- ->getForm();
693-
694675 .. code-block :: php-symfony
695676
696677 // src/Controller/DefaultController.php
@@ -721,6 +702,25 @@ option when building each field:
721702 }
722703 }
723704
705+ .. code-block :: php-standalone
706+
707+ use Symfony\Component\Form\Extension\Core\Type\DateType;
708+ use Symfony\Component\Form\Extension\Core\Type\TextType;
709+ use Symfony\Component\Validator\Constraints\NotBlank;
710+ use Symfony\Component\Validator\Constraints\Type;
711+
712+ $form = $formFactory->createBuilder()
713+ ->add('task', TextType::class, [
714+ 'constraints' => new NotBlank(),
715+ ])
716+ ->add('dueDate', DateType::class, [
717+ 'constraints' => [
718+ new NotBlank(),
719+ new Type(\DateTime::class),
720+ ],
721+ ])
722+ ->getForm();
723+
724724 When the form is bound, these validation constraints will be applied automatically
725725and the errors will display next to the fields on error.
726726
0 commit comments