Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
use-soflomo-purifier.md

install zf3 skeleton

composer create-project -sdev zendframework/skeleton-application zf3

install zendframework/zend-mvc-i18n

composer require zendframework/zend-mvc-i18n

install zendframework/zend-form

composer require zendframework/zend-form

install soflomo/purifier

composer require soflomo/purifier:2.0.0-beta

register to config/modules.config.php

return [
    'Zend\Form',
    'Zend\InputFilter',
    'Zend\Filter',
    'Zend\Hydrator',
    'Zend\Router',
    'Zend\Validator',
    'Application',
    'Soflomo\Purifier',
];

create a form

<?php

namespace Application\Form;

use Zend\Form\Element\Text;
use Zend\Form\Form;
use Zend\InputFilter\InputFilterProviderInterface;

class SampleForm extends Form implements InputFilterProviderInterface
{
      public function init()
      {
            $this->add([
                  'name' => 'sampleinput',
                  'type' => Text::class,
                  'options' => [
                       'label' => 'sample input: '
                   ],
            ]);

            $this->add(array(
                'name' => 'submit',
                'type' => 'Submit',
                'attributes' => array(
                    'value' => 'Go',
                    'id' => 'submitbutton',
                ),
            ));
      }

      public function getInputFilterSpecification()
      {
            return [
                [
                    'name' => 'sampleinput',
                    'required' => true,
                    'filters' => [
                        ['name' => 'htmlpurifier'],
                    ],
                ],
            ];
      }
}

inject controller with the form

<?php
namespace Application;

return [
     // ...
     'controllers' => [
        'factories' => [
            Controller\IndexController::class => function ($container) {
                  return new Controller\IndexController(
                       $container->get('FormElementManager')->get(Form\SampleForm::class)
                  );
            },
        ],
    ],
];

Use in the controller

<?php

namespace Application\Controller;

use Application\Form\SampleForm;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class IndexController extends AbstractActionController
{
    private $form;

    public function __construct(SampleForm $form)
    {
        $this->form = $form;
    }

    public function indexAction()
    {
        $request = $this->getRequest();
        if ($request->isPost()) {

            $this->form->setData($request->getPost());
            var_dump($this->form->isValid());

        }

        return new ViewModel(['form' => $this->form]);
    }
}

applied in view

$form->prepare();

echo $this->form()->openTag($form);
echo $this->formRow($form->get('sampleinput'));
echo $this->formRow($form->get('submit'));
echo $this->form()->closeTag();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment