PHP code example of lexal / http-stepped-form

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

    

lexal / http-stepped-form example snippets


   use Lexal\HttpSteppedForm\Settings\FormSettingsInterface;
   use Lexal\SteppedForm\Step\StepKey;

   final class FormSettings implements FormSettingsInterface
   {
       public function getStepUrl(StepKey $key): string
       {
           // return step URL
       }
 
       public function getUrlBeforeStart(): string
       {
           // returns a URL to redirect to when there is no renderable step
       }

       public function getUrlAfterFinish(): string
       {
           // return a URL to redirect to when the form is finished
       }
   }

   $formSettings = new FormSettings();
   

   use Lexal\HttpSteppedForm\Routing\RedirectorInterface;
   use Symfony\Component\HttpFoundation\Response;

   final class Redirector implements RedirectorInterface
   {
       public function redirect(string $url, array $errors = []): Response
       {
           // create and return redirect response
       }
   }

   $redirector = new Redirector();
   

   use Lexal\HttpSteppedForm\Renderer\RendererInterface;
   use Lexal\SteppedForm\Entity\TemplateDefinition;
   use Symfony\Component\HttpFoundation\Response;

   final class Renderer implements RendererInterface
   {
       public function render(TemplateDefinition $definition): Response
       {
           // create and return response
       }
   }

   $renderer = new Renderer();
   

   use Lexal\HttpSteppedForm\ExceptionNormalizer\ExceptionNormalizer;
   use Lexal\HttpSteppedForm\ExceptionNormalizer\Normalizers\AlreadyStartedExceptionNormalizer;
   use Lexal\HttpSteppedForm\ExceptionNormalizer\Normalizers\DefaultExceptionNormalizer;
   use Lexal\HttpSteppedForm\ExceptionNormalizer\Normalizers\EntityNotFoundExceptionNormalizer;
   use Lexal\HttpSteppedForm\ExceptionNormalizer\Normalizers\FormNotStartedExceptionNormalizer;
   use Lexal\HttpSteppedForm\ExceptionNormalizer\Normalizers\StepNotFoundExceptionNormalizer;
   use Lexal\HttpSteppedForm\ExceptionNormalizer\Normalizers\StepNotRenderableExceptionNormalizer;
   
   $normalizer = new ExceptionNormalizer([
       new AlreadyStartedExceptionNormalizer($redirector),
       new EntityNotFoundExceptionNormalizer($redirector),
       new FormNotStartedExceptionNormalizer($redirector),
       new StepNotRenderableExceptionNormalizer(),
       new StepNotFoundExceptionNormalizer(),
       new DefaultExceptionNormalizer(),
   ]);
   

   use Lexal\HttpSteppedForm\SteppedForm;

   $form = new SteppedForm(
       /* a base stepped form from the step 1 */,
       $formSettings,
       $redirector,
       $renderer,
       $normalizer,
   );
   

   /*
    * Starts a new form session.
    * Returns redirect response to the next step or URL after form finish.
    */
   $form->start(
       /* an entity to initialize a form state */,
       /* unique session key if you need to split different sessions of one form */,
   );

   /* Renders step by its definition */
   $form->render('key');

   /*
    * Handles a step logic and saves a new form state.
    * Returns redirect response to the next step or URL after form finish.
    */
   $form->handle('key', /* request instance*/);

   /* Cancels form session and returns redirect response to the given URL */
   $form->cancel(/* any URL */);
   

use Lexal\HttpSteppedForm\Settings\FormSettingsInterface;
use Lexal\HttpSteppedForm\ExceptionNormalizer\ExceptionNormalizerInterface;
use Lexal\SteppedForm\Exception\AlreadyStartedException;
use Lexal\SteppedForm\Exception\SteppedFormException;
use Symfony\Component\HttpFoundation\Response;

final class CustomExceptionNormalizer implements ExceptionNormalizerInterface
{
    public function supportsNormalization(SteppedFormException $exception): bool
    {
        return $exception instanceof AlreadyStartedException;
    }
    
    public function normalize(SteppedFormException $exception, FormSettingsInterface $formSettings): Response
    {
        // return custom response object
        return new Response();
    }
}