Symfony2 Form - PowerPoint PPT Presentation

symfony2 form n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Symfony2 Form PowerPoint Presentation
Download Presentation
Symfony2 Form

play fullscreen
1 / 20
Symfony2 Form
318 Views
Download Presentation
emera
Download Presentation

Symfony2 Form

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Symfony2 Form • Symfony2 Form • Form basic • Form + Document/Entity • Form type with sub form • Form created in Controller • Tips about Form

  2. Symfony2 Form - Form Type • http://symfony.com/doc/current/book/forms.html#built-in-field-types • Text field: text, email. url, integer, textarea… • Choice Fields: choice, entity, • Date and Time Fields: date, datetime… • Other Fields: checkbox, file… • Field Groups: collection, repeated • Hidden Fields: hidden, csrf • Base Fields: field, form

  3. Symfony2 Form - Create Form //Controller • $this->createForm( $type, $data, $option ) • $this->createFormBuilder( $data, $option )

  4. Symfony2 Form - View Form //Show all field <form action="{{ path('task_new') }}" method="post" {{ form_enctype(form) }}> {{ form_widget(form) }} </form> //Show some field <form action="{{ path('task_new') }}" method="post" {{ form_enctype(form) }}> {{ form_errors(form) }} {{ form_row(form.task) }} {{ form_row(form.dueDate) }} {{ form_rest(form) }} </form>

  5. Symfony2 Form - View Form //Show one field <form action=“" method="post" {{ form_enctype(form) }}> <div> {{ form_label(form.name) }} {{ form_errors(form.name) }} {{ form_widget(form.name) }} </div> <div> {{ form_label(form.gender) }} {{ form_errors(form.gender) }} {{ form_widget(form.gender) }} </div> </form>

  6. Symfony2 Form - View Form • Twig Form Function : http://symfony.com/doc/current/reference/forms/twig_reference.html • Form theme: http://symfony.com/doc/current/book/forms.html#form-theming

  7. Symfony2 Form - Field Type //FormType • $field -> add( $sFieldName, $sFieldType, $aOptions ); • $sFieldName: string - ‘username’, ‘email’, ‘gender’ … • $ sFieldType: string - ‘text’, ‘textarea’, ‘choice’ … • $aOptions: array – label, required …

  8. Form basic -> Demo //Controller $form = $this->createForm(new TestType()); return $this->render( ‘DemoBundle:Test:form.html.twig', array ( 'form' => $form->createView() ));

  9. Form basic -> Demo //Form Type $builder->add( 'name', 'text', array ( 'label' => 'Name', ‘required' => false //Default: true ) ); $builder->add( 'gender', 'choice', array( 'label' => 'Gender', 'choices' => array( 'm' => 'male', 'f' => 'female' ) ) ); $builder->add( 'joinedDate', 'date', array( 'label' => 'Joined Date', ) );

  10. Form + Model //Controller $form = $this->createForm(new TestType(), new User() ); if( $request-> getMethod() == 'POST' ) { $form->bindRequest( $request ); if( $form->isValid() ) { //Validate form var_dump($form->getData()); } } return $this->render( ‘DemoBundle:Test:form.html.twig', array( 'form' => $form->createView() ) );

  11. Form + Model //Form Type public function getDefaultOptions( array $options ) { return array ( 'data_class' => 'Chorus\UserBundle\Document\User', ); } => Validated by form & Document //Form Type public function getDefaultOptions( array $options ) { return array ( • // 'data_class' => 'Chorus\UserBundle\Document\User', ); } => Only validated by Form

  12. Form + Sub Form - Controller $form = $this->createForm(new TestType(), new User() ); if( $request-> getMethod() == 'POST' ) { $form->bindRequest( $request ); //Validate form: based on TestType if( $form->isValid() ) { var_dump($form->getData()); } } return $this->render( ‘DemoBundle:Test:form.html.twig', array ( 'form' => $form->createView() ) );

  13. Form + Sub Form - FormType public function buildForm( FormBuilder $builder, array $options ) { … $builder->add('meta', new UserMetaType()); $builder->add('meta', new UserRoles()); ... }

  14. Form created in Controller $form = $this->createFormBuilder() ->add( 'oldPassword', 'password', array( 'label' => 'Old Password' ) ) ->add( 'password', 'repeated', array( 'type' => 'password', 'invalid_message' => 'The password fields must match.', )) ->getForm(); return $this->render( 'ChorusBackendBundle:User:profile-change-password.html.twig', array( 'form' => $form->createView() ) );

  15. Form Tips • Pass option parameters to Form • Form type do with Document • Form type do with Entity

  16. Tips – Pass option parameters to Form //Controller $form = $this->createForm( new UserType(), new User(), array( ‘bIsDemo‘ => true ) );

  17. Tips – Pass option parameters to Form //UserType public function buildForm( FormBuilder $builder, array $options ) { … if ( isset($options[‘bIsDemo’]) && $options[‘bIsDemo’] ) { //Show some field to run demo } …. }

  18. Tips – Pass option parameters to Form //UserType public function getDefaultOptions( array $options ) { return array( ‘data_class’ => 'Chorus\UserBundle\Document\User‘, ‘bIsDemo' => false ); }

  19. Tips – Form type do with Document //Controller $this->createFormBuilder() ->add('category', 'document', array( 'class' => 'Chorus\PortfolioBundle\Document\Category', 'property' => 'name', 'label' => 'Category', 'empty_value' => 'Select a category‘, 'query_builder' => function(DocumentRepository $dr) use ($aAddedCategories) { return $dr->createQueryBuilder() ->field('id')->notIn( $aAddedCategories); }, ))

  20. Tips – Form type do with Entity //Form Type $builder->add( 'category', 'entity', array ( 'label' => Category', 'multiple' => false, 'property' => 'name', 'class' => ‘DemoCategoryBundle:Category', 'query_builder' => function(EntityRepository $er) { return $er->createQueryBuilder( 'c' ) ->where( 'c.parent_id = :parent' ) ->setParameters( array ( 'parent' => 0, ) ); } ) );