zenify/modular-form-renderer

This package is abandoned and no longer maintained. No replacement package was suggested.
There is no license information available for the latest version (v0.0.2) of this package.

v0.0.2 2015-10-16 13:21 UTC

This package is not auto-updated.

Last update: 2015-12-31 22:29:09 UTC


README

Build Status Quality Score Code Coverage Downloads this Month Latest stable

When you need more than one form renderer and you don't want to cook them to spagetti.

Install

Via Composer:

$ composer require zenify/modular-form-renderer

Usage

Simple create own class that would implement Zenify\ModularFormRenderer\Contract\Forms\Decorator\FormDecoratorInterface.

This CrazySelectJqueryFormDecorator would add crazy-select class to every select box in the form. May be handy as support some javascript addon that would turn select into rich selection tool.

use Nette\Forms\Controls\SelectBox;
use Nette\Forms\Form;
use Zenify\ModularFormRenderer\Contract\Forms\Decorator\FormDecoratorInterface;


final class CrazySelectJqueryFormDecorator implements FormDecoratorInterface
{

    /**
     * {@inheritdoc}
     */
    public function decorate(Form $form)
    {
        foreach ($form->getControls() as $baseControl) {
            if ($baseControl instanceof SelectBox) {
                $baseControl->setAttribute('class', 'crazy-select');
            }
        }
    }


    /**
     * {@inheritdoc}
     */
    public function getWrappers()
    {
        // we need no custom wrappers
        return [];
    }

}

Then we need to add this decorator to renderer. This way, we can add multiple separated decorators and every single one will be applied.

use Nette\Application\UI\Form;
use Zenify\ModularFormRenderer\Forms\Rendering\FormDecoratorRenderer;


class MyPresenter ...
{

    /**
     * @return Form
     */
    protected function createComponentSelectForm()
    {
        $formDecoratorRenderer = new FormDecoratorRenderer;
        $formDecoratorRenderer->addFormDecorator(new MyOwnFormDecorator);

        $form = new Form;
        $form->addSelect('category', 'Category');
        $form->setRenderer($formDecoratorRenderer);
        return $form;
    }

}

Now all select boxes will have our class crazy-select!

Many forms with same settings

If you have many forms, you'd have to add this renderer to every single one. That too much work, right?

Simple FormFactory will save us.

use Nette\Forms\Form;
use Nette\Forms\IFormRenderer;
use Zenify\ModularFormRenderer\Forms\Rendering\FormDecoratorRenderer;


final class FormFactory 
{

    /**
     * @returns Form
     */
    public function create()
    {
        $form = new Form;
        $form->setRenderer($this->createFormRenderer());
        return $form;
    }


    /**
     * @return IFormRenderer
     */
    private function createFormRenderer()
    {
        $formDecoratorRenderer = new FormDecoratorRenderer;

        $formDecoratorRenderer->addFormDecorator(new MyOwnFormDecorator);
        // here you can add some other decorators, or use conditions
        // e.g. add Bootstrap3FormDecorator only for admin forms 

        return $formDecoratorRenderer;
    }

}

Then register as service in config.neon:

services:
    - FormFactory

And use in presenter:

use FormFactory;
use Nette\Application\UI\Form;
use Zenify\ModularFormRenderer\Forms\Rendering\FormDecoratorRenderer;


class MyPresenter ...
{

    /**
     * @var FormFactory
     */
    private $formFactory;


    public function __construct(FormFactory $formFactory)
    {
        $this->formFactory = $formFactory;
    }

    /**
     * @return Form
     */
    protected function createComponentSelectForm()
    {
        $form = $this->formFactory->create();
        // here we can omit all "setup" code
        $form->addSelect('category', 'Category');

        return $form;
    }

}

Testing

$ phpunit

Contributing

Please see CONTRIBUTING for details.