Task: We specify the quantity in the form -> we transfer this quantity to another controller -> we call the payment of the robokassa -> the robokassa returns success -> we write this quantity to the database. I can not figure out how to transfer the form values ​​to another controller. If you transfer to your own, then everything works. And vice versa, no. And also, how to transfer success to the controller? How does he take it?

UPD.

I create a form, display it on screen in twig

public function donutAction(Request $request, $donut_success = 0){ //Создаем форму $form = $this->createFormBuilder($donut) //->add('id_server', 'integer', array('label' => 'Выберите ваш сервер')) ->add('sum','integer', array('label' => 'Количество шариков')) ->add('id_server', 'entity', array( 'label' => 'Выберите ваш сервер', 'attr' => array('class' => 'browser-default'), 'required' => false, 'class' => 'M4MinecraftBundle:Mc_server', 'query_builder' => function(EntityRepository $ems_select) use($id_user){ return $ems_select->createQueryBuilder('s') ->where('s.id_user IN (:id_user)') ->setParameter('id_user', $id_user);}, 'property'=> 'name' )) ->getForm(); } 

After that, when I click on SUBMIT, I switch to the function of another controller

  public function indexAction($sum){ } 

How do I make this function take the value of 'sum' from the form I sent to it?

 $sum=$_REQUEST['sum']; 

Does not work. $request->getData also tried. There is another $form->bind($request); . It works for me in one controller, but not in it.

  • It is much better when the question indicates the part of the code with which the problem occurred. I can assume that the form only needs to change the action attribute. - Fomina Ekaterina
  • No, the action form is on the new controller. I just don’t know how to take form values ​​with this controller. Suppose in the form I have the value of sum. I am writing the function public function indexAction ($ sum) {/ * code for working with sum * /} I cannot work with sum, because it does not see it. Tried to do through $ form-> getData (); But I didn’t quite understand how to do it right - Maksim Savichev
  • You can always get values ​​through $ _POST or $ _GET arrays. More specifically, I can not answer, because there is no code. - Fomina Catherine
  • Look again at the question. I changed it - Maksim Savichev
  • And the form processing documentation did not try to look at: Symfony 2 Forms ? - Alex Krass

2 answers 2

In general, it is better to process forms in the same controller in which you prepare their output. This is a common practice in symfony. It allows you not to duplicate the form code in two different controllers.

If you still need to solve your problem in this way, then you should take the form into a separate class . Then generate a form for output in one controller and use the same form for processing in another. Handle the form as follows:

 public function indexAction(Request $request) { $donut = new Donut(); $form = $this->createForm(new YourFormType(), $donut); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $em = $this->getDoctrine()->getManager(); $em->persist($donut); $em->flush(); } // Делать дальше все, что вздумается } 

$form->isSubmitted() is used here solely for aesthetic pleasure. The code in this case becomes more visual and understandable. This condition is re-checked in $form->isValid() , but it allows the programmer who reads the code to understand that the form was sent without any checks for the POST method or the presence in the form name request, this is not for the controller, the code becomes more understandable .

    As I understand the question is not how to process the form in another controller, but how to get POST data in another controller.

    This is implemented quite simply:

     public function indexAction(\Symfony\Compoment\HttpFoundation\Request $request) { // Получаем переданные из формы данные $post_data = $request->request->all(); } 

    In this case, data validation falls on you. How to do array validation can be read here:

     http://symfony.com/doc/current/validation.html http://stackoverflow.com/questions/16050240/how-can-i-validate-array-keys-using-symfony-validation 

    I would also recommend that you separate the logic into independent services and transfer the form to a separate class. In this case, you do not need a separate controller, you receive data in the main one and transfer it to the service after validation.