Skip to content

akrabat/rka-slim-zendform

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Zend\Form integration with Slim 3

This service provider integrates Zend\Form into a Slim 3 application.

Usage

  1. composer require slim/twig-view

  2. composer require akrabat/rka-slim-zendform

  3. Register Twig-View as per the README

  4. Register the FormProvider in index.php:

     $app->getContainer()->register(new RKA\Form\FormProvider);
    
  5. Create a form:

     <?php
     namespace RKA;
    
     use Zend\Form\Form;
     use Zend\InputFilter\InputFilterProviderInterface;
    
     class ExampleForm extends Form implements InputFilterProviderInterface
     {
         public function init()
         {
             $this->add([
                 'name' => 'email',
                 'options' => [
                     'label' => 'Email address',
                 ],
                 'attributes' => [
                     'id'       => 'email',
                     'class'    => 'form-control',
                     'required' => 'required',
                 ],
             ]);
    
             $this->add([
                 'name' => 'submit',
                 'type' => 'button',
                 'options' => [
                     'label' => 'Go!',
                 ],
                 'attributes' => [
                     'class' => 'btn btn-default',
                 ],
             ]);
         }
    
         public function getInputFilterSpecification()
         {
             return [
                 'email' => [
                     'required' => true,
                     'filters'  => [
                         ['name' => 'StringTrim'],
                         ['name' => 'StripTags'],
                     ],
                     'validators' => [
                         ['name' => 'EmailAddress'],
                     ],
                 ],
             ];
         }
     }
    
  6. Example action:

     $app->map(['GET', 'POST'], '/', function ($request, $response) {
         $sm = $this['serviceManager'];
         $formElementManager = $sm->get('FormElementManager');
         $form = $formElementManager->get("RKA\ExampleForm");
    
         if ($request->isPost()) {
             $data = $request->post();
             $form->setData($data);
             $isValid = $form->isValid();
             if ($form->isValid()) {
                 echo "Success!";
                 exit;
             }
         }
    
         $this['view']->render($response, 'home.twig', array(
             'form' => $form
         ));
         return $response;
     });
    

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages