Skip to content

Latest commit

 

History

History
46 lines (36 loc) · 1.44 KB

021_Using_custom_controller.md

File metadata and controls

46 lines (36 loc) · 1.44 KB

Using Custom Controller

If a custom action is given the form will add a hidden field named form_referer so you can trace back the original object afterwards.

In the controller you can use the submitted data (and files) or validate them first.

<?php

namespace App\Controller;

use Pimcore\Model\DataObject\SimpleForm;
use SimpleFormsBundle\Form\SimpleFormType;
use SimpleFormsBundle\Service\SimpleFormService;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class FormController extends BaseController
{
    /**
     * @Route("/form/hello", name="form_examples_hello")
     *
     * @param Request $request
     * @return Response
     */
    public function welcomeAction(Request $request, FormFactoryInterface $formFactory, SimpleFormService $service)
    {
        if ($formId = $request->get('simple_form')['form_referer']) {
            $formObject = SimpleForm::getById($formId);
            $form = $formFactory->createBuilder(SimpleFormType::class, $formObject)->getForm();
            $form->handleRequest($request);

            if ($form->isSubmitted() && $form->isValid()) {
                $data = $service->parseDataAsArray($request->get(SimpleFormType::PREFIX));

                return new Response("Hello {$data['name']}!");
            }
        }
        

        return new Response();
    }
}