SlideShare a Scribd company logo
1 of 1
Download to read offline
How Symfony2 Forms Works?
                                                                                                                                                                                                                                                               Basic usage
                                                                                          Built-in Field Types
                                                                                          Text Fields   Choice Fields       Date and Time        Field Groups
                                                                                                                                                                             Fragments Templates
                                                                                                                                                                             The fragments are defined as blocks in Twig and as template files in PHP.     {% block field_row %}




                                                                                                                                                                                                                                                                                          TWIG
                                                                                          text          choice              Fields               collection                                                                                                  <div class="form_row">
                                                                                          textarea      entity              date                 repeated                    label    (e.g. field_label) renders the field's label                             {{ form_label(form) }}
                                                                                          email         country             datetime                                         widget   (e.g. field_widget)renders the field's HTML representation               {{ form_errors(form) }}
                      Data is Valid                   Data is invalid                     integer       language            time                 Hidden Fields               errors   (e.g. field_errors)renders the field's errors                            {{ form_widget(form) }}
                                                                                          money         locale              birthday             hidden                      row      (e.g. field_row)   renders the field's entire row                      </div>
               Form is bound and you can            Form is bound and
               for e.g. persist data to DB          rendered, displaying                  number        timezone                                 csrf                                                    (label, widget & errors)                          {% endblock field_row %}
               before redirecting the user          all validation errors                 password                          Other Fields                                     rows    (e.g. field_rows)   renders all form field rows
               to other page                                                              percent                           checkbox             Base Fields                 rest    (e.g. form_rest)    renders any fields that have not yet been rendered
                                                                                          search                            file                 field                       enctype (e.g. form_enctype) if at least one field is a file upload field, this
                                                                                          url                               radio                form                                                    renders the enctype="multipart/form-data”




     Entity                      represent and
                                                                                                          Controller                                                                                               The Form
                                 store the data                         Validation
                                                                        Applied to the                                                                                                                                  Task         Write a blog post
                                                                        underlying object (class)
      src/Acme/TaskBundle/Entity/Task.php                                                                 src/Acme/TaskBundle/Controller/DefaultController.php                                                                        Jul         24       ,    2011
                                                                        by adding a set of                                                                                                                              Duedate
                                                                        rules (constraints).
                                                                        These rules can                      $task = new Task();                                                                                         Submit
       namespace AcmeTaskBundleEntity;
                                                                        be specified in                      $task->setTask('Write a blog post');
                                                                        YAML, XML,                           $task->setDueDate(new DateTime('tomorrow'));
       use SymfonyComponentValidatorConstraints as Assert;           annotations,                                                                               Create
                                                                        or PHP                                                                                     the form
       class Task{                                                                                           $form = $this->createFormBuilder($task)                                                                                                   render
         /**                                                                                                     ->add('task', 'text')                           Built-in Field                                                                        the
          * @AssertNotBlank()                using annotations                                                  ->add('dueDate', 'date')                        Types                                                                                 form
          */                                  for constraints           bind the submitted data to               ->getForm();
         public $task;                                                  the form, which translates
                                                                        that data back to the task
                                                                        and dueDate properties of              if ($request->getMethod() == 'POST') {
           /**
           * @AssertNotBlank()
                                                                        the $task object
                                                                                                                   $form->bindRequest($request);
                                                                                                                                                                                                            View
           * @AssertType("DateTime")                                  ask the $task object whether
                                                                        or not it has valid data
           */                                                                                                     if ($form->isValid()) {
           protected $dueDate;                                                                                                                                                                               src/Acme/TaskBundle/Resources/views/Default/new.html.twig
                                                                                                                       // perform some action, such as saving the task to DB
                                                                                                                       return $this->redirect($this->generateUrl('task_success'));                           <form action="{{ path('task_new') }}" method="post"




                                                                                                                                                                                                                                                                                   TWIG
           public function getTask() {                                                                                                                                                                                                 {{ form_enctype(form) }}>
                                                                                                                   }
             return $this->task;                                                                                                                                                                               {{ form_widget(form) }}
           }                                                                                                  }
           public function setTask($task) {                             Unless a property is                                                                                                                   <input type="submit" />
             $this->task = $task;                                       public, it must have                                                                                                                 </form>
                                                                                                             return $this->render('AcmeTaskBundle:Default:new.html.twig',
           }                                                            a "getter" and "setter"
                                                                        method so that the                        array(                                                                                                                      OR
                                                                        form component can                            'form' => $form->createView(),                                                         src/Acme/TaskBundle/Resources/views/Default/new.html.php
           public function getDueDate() {                               get and put data onto
             return $this->dueDate;                                     the property                              ));
                                                                                                                                                                                                             <form action="<?php echo $view['router']->generate('task_new') ?>"
           }




                                                                                                                                                                                                                                                                                   PHP
                                                                                                                                                                                                                  method="post" <?php echo $view['form']->enctype($form) ?> >
           public function setDueDate(DateTime $dueDate = null) {                                                                                                                                             <?php echo $view['form']->widget($form) ?>
             $this->dueDate = $dueDate;                                                                   bindRequest                         isValid                 createView
           }                                                                                              translate user-submitted            check for valid         render the
                                                                                                          data back to the                    data in the             form                                     <input type="submit" />
       }                                                                                                  properties of an object             object                                                         </form>



Examples from: http://symfony.com/doc/current/book/forms.html                                                                                                                                                                                            andreiabohner.wordpress.com

More Related Content

Viewers also liked (15)

Sueño
SueñoSueño
Sueño
 
Cond present+corrige
Cond present+corrigeCond present+corrige
Cond present+corrige
 
Project graffics oct 2015 word resume
Project graffics oct 2015 word resumeProject graffics oct 2015 word resume
Project graffics oct 2015 word resume
 
Diego guevara
Diego guevaraDiego guevara
Diego guevara
 
Apache - Fileroom Form
Apache - Fileroom FormApache - Fileroom Form
Apache - Fileroom Form
 
Autodesk Certificate 2013 P3D
Autodesk Certificate 2013 P3DAutodesk Certificate 2013 P3D
Autodesk Certificate 2013 P3D
 
سب دوستوں کو شب بخیر
سب دوستوں کو شب بخیرسب دوستوں کو شب بخیر
سب دوستوں کو شب بخیر
 
IT decision making sample
IT decision making sampleIT decision making sample
IT decision making sample
 
8.5x11 flyer dodgers2015
8.5x11 flyer dodgers20158.5x11 flyer dodgers2015
8.5x11 flyer dodgers2015
 
Justification1
Justification1Justification1
Justification1
 
Test
TestTest
Test
 
Yo sr
Yo srYo sr
Yo sr
 
Unit 1 research methods tmanston
Unit 1 research methods tmanstonUnit 1 research methods tmanston
Unit 1 research methods tmanston
 
Liu riwang, agenda 14 15
Liu riwang, agenda 14 15Liu riwang, agenda 14 15
Liu riwang, agenda 14 15
 
La economía
La economíaLa economía
La economía
 

Similar to How symfony2 forms_works

Zpugdc deformpresentation-100709203803-phpapp01
Zpugdc deformpresentation-100709203803-phpapp01Zpugdc deformpresentation-100709203803-phpapp01
Zpugdc deformpresentation-100709203803-phpapp01
Jeffrey Clark
 
BMID training - Mobyle Workshop - September 28, 2012
BMID training  - Mobyle Workshop - September 28, 2012BMID training  - Mobyle Workshop - September 28, 2012
BMID training - Mobyle Workshop - September 28, 2012
Hervé Ménager
 
Artefactos biz talk 2006
Artefactos biz talk 2006Artefactos biz talk 2006
Artefactos biz talk 2006
Sandro Pereira
 
Struts Intro Course(1)
Struts Intro Course(1)Struts Intro Course(1)
Struts Intro Course(1)
wangjiaz
 

Similar to How symfony2 forms_works (18)

Zpugdc deformpresentation-100709203803-phpapp01
Zpugdc deformpresentation-100709203803-phpapp01Zpugdc deformpresentation-100709203803-phpapp01
Zpugdc deformpresentation-100709203803-phpapp01
 
BMID training - Mobyle Workshop - September 28, 2012
BMID training  - Mobyle Workshop - September 28, 2012BMID training  - Mobyle Workshop - September 28, 2012
BMID training - Mobyle Workshop - September 28, 2012
 
Intake 38 data access 5
Intake 38 data access 5Intake 38 data access 5
Intake 38 data access 5
 
Artefactos biz talk 2006
Artefactos biz talk 2006Artefactos biz talk 2006
Artefactos biz talk 2006
 
Intake 37 ef2
Intake 37 ef2Intake 37 ef2
Intake 37 ef2
 
iOS Application Development
iOS Application DevelopmentiOS Application Development
iOS Application Development
 
CCK For Code Junkies
CCK For Code JunkiesCCK For Code Junkies
CCK For Code Junkies
 
Accessing loosely structured data from F# and C#
Accessing loosely structured data from F# and C#Accessing loosely structured data from F# and C#
Accessing loosely structured data from F# and C#
 
Drupal 7 field API
Drupal 7 field APIDrupal 7 field API
Drupal 7 field API
 
Week8
Week8Week8
Week8
 
Maxbox starter19
Maxbox starter19Maxbox starter19
Maxbox starter19
 
RomaFramework Tutorial Basics
RomaFramework Tutorial BasicsRomaFramework Tutorial Basics
RomaFramework Tutorial Basics
 
Choose'10: Ralf Laemmel - Dealing Confortably with the Confusion of Tongues
Choose'10: Ralf Laemmel - Dealing Confortably with the Confusion of TonguesChoose'10: Ralf Laemmel - Dealing Confortably with the Confusion of Tongues
Choose'10: Ralf Laemmel - Dealing Confortably with the Confusion of Tongues
 
JavaFX fundamentals
JavaFX fundamentalsJavaFX fundamentals
JavaFX fundamentals
 
21 domino mohan-1
21 domino mohan-121 domino mohan-1
21 domino mohan-1
 
Struts Intro Course(1)
Struts Intro Course(1)Struts Intro Course(1)
Struts Intro Course(1)
 
Using SPMetal for faster SharePoint development
Using SPMetal for faster SharePoint developmentUsing SPMetal for faster SharePoint development
Using SPMetal for faster SharePoint development
 
Drupal 8: Fields reborn
Drupal 8: Fields rebornDrupal 8: Fields reborn
Drupal 8: Fields reborn
 

How symfony2 forms_works

  • 1. How Symfony2 Forms Works? Basic usage Built-in Field Types Text Fields Choice Fields Date and Time Field Groups Fragments Templates The fragments are defined as blocks in Twig and as template files in PHP. {% block field_row %} TWIG text choice Fields collection <div class="form_row"> textarea entity date repeated label (e.g. field_label) renders the field's label {{ form_label(form) }} email country datetime widget (e.g. field_widget)renders the field's HTML representation {{ form_errors(form) }} Data is Valid Data is invalid integer language time Hidden Fields errors (e.g. field_errors)renders the field's errors {{ form_widget(form) }} money locale birthday hidden row (e.g. field_row) renders the field's entire row </div> Form is bound and you can Form is bound and for e.g. persist data to DB rendered, displaying number timezone csrf (label, widget & errors) {% endblock field_row %} before redirecting the user all validation errors password Other Fields rows (e.g. field_rows) renders all form field rows to other page percent checkbox Base Fields rest (e.g. form_rest) renders any fields that have not yet been rendered search file field enctype (e.g. form_enctype) if at least one field is a file upload field, this url radio form renders the enctype="multipart/form-data” Entity represent and Controller The Form store the data Validation Applied to the Task Write a blog post underlying object (class) src/Acme/TaskBundle/Entity/Task.php src/Acme/TaskBundle/Controller/DefaultController.php Jul 24 , 2011 by adding a set of Duedate rules (constraints). These rules can $task = new Task(); Submit namespace AcmeTaskBundleEntity; be specified in $task->setTask('Write a blog post'); YAML, XML, $task->setDueDate(new DateTime('tomorrow')); use SymfonyComponentValidatorConstraints as Assert; annotations, Create or PHP the form class Task{ $form = $this->createFormBuilder($task) render /** ->add('task', 'text') Built-in Field the * @AssertNotBlank() using annotations ->add('dueDate', 'date') Types form */ for constraints bind the submitted data to ->getForm(); public $task; the form, which translates that data back to the task and dueDate properties of if ($request->getMethod() == 'POST') { /** * @AssertNotBlank() the $task object $form->bindRequest($request); View * @AssertType("DateTime") ask the $task object whether or not it has valid data */ if ($form->isValid()) { protected $dueDate; src/Acme/TaskBundle/Resources/views/Default/new.html.twig // perform some action, such as saving the task to DB return $this->redirect($this->generateUrl('task_success')); <form action="{{ path('task_new') }}" method="post" TWIG public function getTask() { {{ form_enctype(form) }}> } return $this->task; {{ form_widget(form) }} } } public function setTask($task) { Unless a property is <input type="submit" /> $this->task = $task; public, it must have </form> return $this->render('AcmeTaskBundle:Default:new.html.twig', } a "getter" and "setter" method so that the array( OR form component can 'form' => $form->createView(), src/Acme/TaskBundle/Resources/views/Default/new.html.php public function getDueDate() { get and put data onto return $this->dueDate; the property )); <form action="<?php echo $view['router']->generate('task_new') ?>" } PHP method="post" <?php echo $view['form']->enctype($form) ?> > public function setDueDate(DateTime $dueDate = null) { <?php echo $view['form']->widget($form) ?> $this->dueDate = $dueDate; bindRequest isValid createView } translate user-submitted check for valid render the data back to the data in the form <input type="submit" /> } properties of an object object </form> Examples from: http://symfony.com/doc/current/book/forms.html andreiabohner.wordpress.com