@@ -444,14 +444,29 @@ builder:
444
444
445
445
.. code-block :: php-symfony
446
446
447
- $defaults = array(
448
- 'dueDate' => new \DateTime('tomorrow'),
449
- );
447
+ // src/Acme/TaskBundle/Controller/DefaultController.php
448
+ namespace Acme\TaskBundle\Controller;
450
449
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
+ }
455
470
456
471
.. tip ::
457
472
@@ -511,16 +526,23 @@ by ``handleRequest()`` to determine whether a form has been submitted):
511
526
512
527
.. code-block :: php-symfony
513
528
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;
515
534
516
- public function searchAction()
535
+ class DefaultController extends Controller
517
536
{
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
+ ));
522
543
523
- // ...
544
+ // ...
545
+ }
524
546
}
525
547
526
548
.. _component-form-intro-handling-submission :
@@ -562,26 +584,34 @@ method:
562
584
563
585
.. code-block :: php-symfony
564
586
565
- // ...
587
+ // src/Acme/TaskBundle/Controller/DefaultController.php
588
+ namespace Acme\TaskBundle\Controller;
566
589
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
568
595
{
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();
573
602
574
- $form->handleRequest($request);
603
+ $form->handleRequest($request);
575
604
576
- if ($form->isSubmitted() && $form->isValid()) {
577
- $data = $form->getData();
605
+ if ($form->isSubmitted() && $form->isValid()) {
606
+ $data = $form->getData();
578
607
579
- // ... perform some action, such as saving the data to the database
608
+ // ... perform some action, such as saving the data to the database
580
609
581
- return $this->redirectToRoute('task_success');
582
- }
610
+ return $this->redirectToRoute('task_success');
611
+ }
583
612
584
- // ...
613
+ // ...
614
+ }
585
615
}
586
616
587
617
This defines a common form "workflow", which contains 3 different possibilities:
@@ -628,20 +658,31 @@ option when building each field:
628
658
629
659
.. code-block :: php-symfony
630
660
661
+ // src/Acme/TaskBundle/Controller/DefaultController.php
662
+ namespace Acme\TaskBundle\Controller;
663
+
664
+ use Symfony\Bundle\FrameworkBundle\Controller\Controller;
631
665
use Symfony\Component\Validator\Constraints\NotBlank;
632
666
use Symfony\Component\Validator\Constraints\Type;
633
667
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
+ }
645
686
646
687
When the form is bound, these validation constraints will be applied automatically
647
688
and the errors will display next to the fields on error.
0 commit comments