PHP code example of solidworx / form-handler-bundle

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

    

solidworx / form-handler-bundle example snippets




// app/AppKernel.php

// ...
    public function registerBundles()
    {
        $bundles = [
            // ...
            new SolidWorx\FormHandler\FormHandlerBundle(),
        ];
        
        // ...
    )


public function getForm(FormFactoryInterface $factory, Options $options);



use Symfony\Component\Form\FormFactoryInterface;
use SolidWorx\FormHandler\FormHandlerInterface;
use SolidWorx\FormHandler\Options;

class MyFormHandler implements FormHandlerInterface
{
    public function getForm(FormFactoryInterface $factory, Options $options)
    {
        // either
        return MyFormType::class;

        // or
        return $factory->create(MyFormType::class);
    }
}

return $factory->create(MyFormType::class, null, ['horizontal' => true]);



class MyController extends Controller
{
    public function addAction()
    {
        return $this->get('solidworx.form_handler')->handle(MyFormHandler::class); // MyFormHandler will automatically be pulled from the container if it is tagges with `form.handler`
    }
}



use SolidWorx\FormHandler\FormRequest;
use SolidWorx\FormHandler\FormHandlerInterface;
use SolidWorx\FormHandler\FormHandlerFailInterface;
use Symfony\Component\Form\FormErrorIterator;

class MyFormHandler implements FormHandlerInterface, FormHandlerFailInterface
{
    // ...
    public function onFail(FormRequest $formRequest, FormErrorIterator $errors, $data = null)
    {
        // Form submission has failed, probably due to a validation error.
        // Handle it here if you need specific custom logic
    }
}



use SolidWorx\FormHandler\FormRequest;
use SolidWorx\FormHandler\FormHandlerInterface;
use SolidWorx\FormHandler\FormHandlerSuccessInterface;

class MyFormHandler implements FormHandlerInterface, FormHandlerSuccessInterface
{
    // ...
    public function onSuccess($data, FormRequest $form)
    {
        // $data is the submitted data from the form, do something with it here
        // This will probably save info to the DB
    }
}



class MyController extends Controller
{
    public function addAction()
    {
        return $this->get('solidworx.form_handler')->handle(MyFormHandler::class, ['entity' => new Blog]); // MyFormHandler will automatically be pulled from the container if it is tagges with `form.handler`
    }
}



use Symfony\Component\Form\FormFactoryInterface;
use SolidWorx\FormHandler\FormHandlerInterface;
use SolidWorx\FormHandler\Options;

class MyFormHandler implements FormHandlerInterface
{
    public function getForm(FormFactoryInterface $factory, Options $options)
    {
        return $factory->create(MyFormType::class, $options->get('entity'));
    }
}



use Symfony\Component\OptionsResolver\OptionsResolver;
use SolidWorx\FormHandler\FormHandlerInterface;
use SolidWorx\FormHandler\FormHandlerOptionsResolver;

class MyFormHandler implements FormHandlerInterface, FormHandlerOptionsResolver
{
    // ...
    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setRequired('entity');
        $resolver->addAllowedTypes('entity', BlogEntity::class);
        $resolver->setDefault('another_options', 'myvalue');
    }
}