PHP code example of mitom / form-handler-bundle

1. Go to this page and download the library: Download mitom/form-handler-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mitom / form-handler-bundle example snippets


    $bundles = [
        // ...
        new Mitom\Bundle\FormHandlerBundle\FormHandlerBundle()
    ];

namespace Acme\TaskBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class TaskType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('task')
            ->add('dueDate', null, ['widget' => 'single_text'])
            ->add('save', 'submit');
    }

    public function getName()
    {
        return 'task';
    }
}

namespace Acme\TaskBundle\Form\Handler;

use Mitom\Bundle\FormHandlerBundle\Handler\AbstractFormHandler;
use Acme\Entity\Task;
use Acme\Entity\Form\Type\TaskType;
use Symfony\Component\Routing\RouterInterface;

class TaskFormHandler extends AbstractFormHandler
{
    private $router;

    public function __construct(RouterInterface $router)
    {
      $this->router = $router;
    }

    /** @inheritDoc */
    public function getType()
    {
        /**
         * In case your FormType is a service, you could just return
         * its' alias here as a string and let the FormFactory create it
         * for you.
         */
        return new TaskType();
    }

    public function onSuccess(FormData $formData)
    {
        /**
         * do whatever you want, like persisting to database
         */

        return new RedirectResponse($this->router->generate('acme.task', ['task' => $formData->getData()->getId()]));
    }

    public function onError(FormData $formData)
    {
        /**
         * do whatever you want, like log the error or simply return the FormData.
         */

        return ['form' => $formData->getForm()->createView()];
    }
}

namespace Acme\TaskBundle\Controller;

use Mitom\Bundle\FormHandlerBundle\FormData;
use Mitom\Bundle\FormHandlerBundle\FormHandlerManager;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class TaskController
{
    protected $formHandlerManager;

    /**
     * @param FormHandlerManager $formHandlerManager
     */
    public function __construct(FormHandlerManager $formHandlerManager)
    {
        $this->formHandlerManager = $formHandlerManager;
    }


    /**
     * @Template()
     */
    public function newAction()
    {
        return ['form' => $this->formHandlerManager()->getHandler('task')->createForm()->createView()];
    }


    /**
     * @Template()
     */
    public function createAction(Request $request)
    {
        $formData = new FormData();
        $formData->setRequest($request);

        // note that you can get the handler by using the name of the FormType
        return $this->formHandlerManager()->getHandler('task')->handle($formData);
    }
}