200 likes | 557 Views
Symfony2 Form. Symfony2 Form Form basic Form + Document/Entity Form type with sub form Form created in Controller Tips about Form. Symfony2 Form - Form Type. http :// symfony.com /doc/current/book/ forms.html#built -in-field-types Text field: text, email. url , integer, textarea …
E N D
Symfony2 Form • Symfony2 Form • Form basic • Form + Document/Entity • Form type with sub form • Form created in Controller • Tips about Form
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
Symfony2 Form - Create Form //Controller • $this->createForm( $type, $data, $option ) • $this->createFormBuilder( $data, $option )
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>
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>
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
Symfony2 Form - Field Type //FormType • $field -> add( $sFieldName, $sFieldType, $aOptions ); • $sFieldName: string - ‘username’, ‘email’, ‘gender’ … • $ sFieldType: string - ‘text’, ‘textarea’, ‘choice’ … • $aOptions: array – label, required …
Form basic -> Demo //Controller $form = $this->createForm(new TestType()); return $this->render( ‘DemoBundle:Test:form.html.twig', array ( 'form' => $form->createView() ));
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', ) );
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() ) );
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
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() ) );
Form + Sub Form - FormType public function buildForm( FormBuilder $builder, array $options ) { … $builder->add('meta', new UserMetaType()); $builder->add('meta', new UserRoles()); ... }
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() ) );
Form Tips • Pass option parameters to Form • Form type do with Document • Form type do with Entity
Tips – Pass option parameters to Form //Controller $form = $this->createForm( new UserType(), new User(), array( ‘bIsDemo‘ => true ) );
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 } …. }
Tips – Pass option parameters to Form //UserType public function getDefaultOptions( array $options ) { return array( ‘data_class’ => 'Chorus\UserBundle\Document\User‘, ‘bIsDemo' => false ); }
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); }, ))
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, ) ); } ) );