PHP code example of symftony / form-handler

1. Go to this page and download the library: Download symftony/form-handler 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/ */

    

symftony / form-handler example snippets




class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            ...
            new Symftony\FormHandler\FormBundle\FormHandlerBundle(),
            ...
        ];
    }
    ...

    public function yourAction(Request $request)
    {
        $formHandler = $this->get('form_handler.form_handler.default');
        // Throw exception if the form is NotSubmit/NotValid/TransformFailed
        $form = $formHandler->createForm(TextType::class, 'my-value', 'My default data', [
            'method' => 'GET',
            'handler_invalid' => true,
            'handler_not_submitted' => true,
            'handler_transformation_failed' => true,
        ]);
        // You get the $form->getData() if all succeed
        $result = $formHandler->handleRequest($form, $request);

        return $this->render('default/index.html.twig', [
            'form' => $form->createView(),
            'result' => $result,
        ]);
    }



namespace AppBundle\Form\Handler;

use Symfony\Component\HttpFoundation\Request;
use Symftony\FormHandler\FormHandler;

class YourFormHandler extends FormHandler
{
    public function createFromRequest(Request $request, $notSubmitted = true, $invalid = true)
    {
        $form = $this->createForm(TextType::class, 'my-value', 'My default data', [
            'handler_not_submitted' => $notSubmitted,
            'handler_invalid' => $invalid,
        ]);

        $result = $this->handleRequest($form, $request);

        // DO whatever you want with your $result
        
        return $result
    }
}

    public function yourAction(Request $request)
    {
        return $this->render('default/index.html.twig', [
            'result' => $this->get('app.form_handler.your')->createFromRequest(
                $request,
                'my data if form wasn\'t post' // result if the form isn't submitted
                true // Gonna throw Exception when the form isn't valid
            ),
        ]);
    }
bash
# Install Composer
curl -sS https://getcomposer.org/installer | php
bash
php composer