@@ -387,22 +387,6 @@ is created from the form factory.
387
387
388
388
.. configuration-block ::
389
389
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
-
406
390
.. code-block :: php-symfony
407
391
408
392
// src/Controller/TaskController.php
@@ -431,6 +415,22 @@ is created from the form factory.
431
415
}
432
416
}
433
417
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
+
434
434
As you can see, creating a form is like writing a recipe: you call ``add() ``
435
435
for each new field you want to create. The first argument to ``add() `` is the
436
436
name 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:
447
447
448
448
.. configuration-block ::
449
449
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
-
467
450
.. code-block :: php-symfony
468
451
469
452
// src/Controller/DefaultController.php
@@ -490,6 +473,23 @@ an "edit" form), pass in the default data when creating your form builder:
490
473
}
491
474
}
492
475
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
+
493
493
.. tip ::
494
494
495
495
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
533
533
534
534
.. configuration-block ::
535
535
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
-
549
536
.. code-block :: php-symfony
550
537
551
538
// src/Controller/DefaultController.php
@@ -567,46 +554,28 @@ by :method:`Symfony\\Component\\Form\\Form::handleRequest` to determine whether
567
554
}
568
555
}
569
556
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
-
580
557
.. code-block :: php-standalone
581
558
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;
586
560
587
561
// ...
588
562
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
+ ]);
597
567
598
- if ($form->isSubmitted() && $form->isValid()) {
599
- $data = $form->getData();
568
+ // ...
600
569
601
- // ... perform some action, such as saving the data to the database
570
+ .. _ component-form-intro-handling-submission :
602
571
603
- $response = new RedirectResponse('/task/success');
604
- $response->prepare($request);
572
+ Handling Form Submissions
573
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
605
574
606
- return $response->send();
607
- }
575
+ To handle form submissions, use the :method: ` Symfony \\ Component \\ Form \\ Form::handleRequest `
576
+ method:
608
577
609
- // ...
578
+ .. configuration-block ::
610
579
611
580
.. code-block :: php-symfony
612
581
@@ -640,6 +609,37 @@ method:
640
609
}
641
610
}
642
611
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
+
643
643
.. caution ::
644
644
645
645
The form's ``createView() `` method should be called *after * ``handleRequest() `` is
@@ -672,25 +672,6 @@ option when building each field:
672
672
673
673
.. configuration-block ::
674
674
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
-
694
675
.. code-block :: php-symfony
695
676
696
677
// src/Controller/DefaultController.php
@@ -721,6 +702,25 @@ option when building each field:
721
702
}
722
703
}
723
704
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
+
724
724
When the form is bound, these validation constraints will be applied automatically
725
725
and the errors will display next to the fields on error.
726
726
0 commit comments