A
A
Alexander2016-10-05 20:36:22
symfony
Alexander, 2016-10-05 20:36:22

How to split the form filling into several steps in symphony2?

Faced with an impossible task for myself, just learning the symphony
, the ordering process is divided into several steps
, you need to somehow save the data from the first steps and transfer them to the subsequent ones,
how could this be done?
without the use of additional bundles (there is no need for complex logic)

<?php

namespace AppBundle\Controller;

use Symfony\Component\DomCrawler\Field\ChoiceFormField;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; //под вопросом
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

//use Timestampable\Fixture\Document\Book;

//функционал формы
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;

use AppBundle\Entity\Book;
use AppBundle\Form\BookType;
use Symfony\Component\HttpFoundation\Response;


class BookController extends Controller
{
    /**
     * @Route("/")
     */
    public function findAction(Request $request)
    {

        $book = new Book();

        //шаг первый - запоняем начальные поля
        $form = $this->createFormBuilder($book)
            ->add('date', DateTimeType::class, array('widget' => 'single_text'))
            ->add('fromBook', ChoiceType::class)
            ->add('toBook', ChoiceType::class)
            ->add('returnDate', DateTimeType::class, array('widget' => 'single_text'))
            ->add('returnFrom', ChoiceType::class)
            ->add('returnTo', ChoiceType::class)
            ->add('people')
            ->add('isReturn', CheckboxType::class)
            ->getForm();

        $form->handleRequest($request);

        if ($form->isSubmitted()) {

            // после отправки формы
            // здесь как то нужно записать наверное данные из формы
            // чтобы можно было использовать эти данные в следующем getcarsAction

            return $this->redirectToRoute('/step2');
        }

        return $this->render('AppBundle:Book:find.html.twig', array(
            'book' => $book,
            'form' => $form->createView(),
        ));

    }

    /**
     * @Route("/step2")
     */
    public function getcarsAction()
    {

        // здесь нужно получить данные с первого шага findAction
        // например записать данные в переменную $getTrips

        $getTrips = $step1; // как в $step1 получить данные - в этом суть вопроса

        if ($form->isSubmitted() && $form->isValid()) {

            return $this->redirectToRoute('/step3');
        }

        return $this->render('AppBundle:Book:cars.html.twig', array(
            'triplist' => $getTrips,
        ));
    }

    /**
     * @Route("/step3")
     */
    public function bookAction()
    {

    }

}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
dizzy7, 2016-10-13
@alexmixaylov

I would make an entity that covers all fields from all steps and a form set per step. Between steps, you save the intermediate result to the database, on the next one you extract and pass it to the form with the set of fields corresponding to this step.

I
Igor, 2016-10-06
@aM-aM

1) I advise you to transfer the logic of Routes to routing.yml.
2) At each step, call your "findAction" method and, depending on the incoming $request, call the method that processes a certain step, or it's better to make a StepController (another class) and execute the logic of the steps in it.

I
ivanovnickolay, 2016-10-13
@ivanovnickolay

Why not use a multi-page form??
In fact, everything is in one form for the symphony, and for the user, the breakdown into stages ..

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question