@@ -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
647688and the errors will display next to the fields on error.
0 commit comments