symfony - How to deal with 3 form on the same page? -
i have page 3 forms , independent (the user can complete 0, 1, 2 or 3) :
- the first asks phone number if user wants called;
- the second same if user wants contacted sms;
- the third used if user want add commentary
what best way handle situation?
should repeat code 3 times in controller?
public function mypageaction(request $request) { // first 1 $form = $this->createformbuilder() ->add... ->getform(); $form->handlerequest($request); if ($form->issubmitted() && $form->isvalid() ) { // code } // second 1 $form2 = $this->createformbuilder() ->add... ->getform(); $form2->handlerequest($request); if ($form2->issubmitted() && $form2->isvalid() ) { // code } // third 1 $form3 = $this->createformbuilder() ->add... ->getform(); $form3->handlerequest($request); if ($form3->issubmitted() && $form3->isvalid() ) { // code } }
and in twig :
i have modal first 1 = user clicks , modal form pops up:
{{ form_start(form) }} {{ form_widget(form) }} {{ form_end(form) }}
same one, other modal :
{{ form_start(form2) }} {{ form_widget(form2) }} {{ form_end(form2) }}
the third 1 @ bottom of page :
{{ form_start(form3) }} {{ form_widget(form3) }} {{ form_end(form3) }}
i think it's lot of code , repeat myself much, can "optimize"? fact first , second form same, can "regroup" them in one? can create "phonetype" form have no other choice call twice in controller?
sorry if there lot of question here, can summarize maybe : what best practice handle multiple form on same page? (in controller , view)
i see following ways of achieving target:
- use compound forms (embed form within global form),
- try using multi-step form, see: https://github.com/craue/craueformflowbundle
- dynamically modify forms using events: https://symfony.com/doc/current/form/dynamic_form_modification.html
but why cannot embed fields in 1 form? splitting 3 fields shooting bee using cannon. recommend using third method because easiest one.
Comments
Post a Comment