定制 zenify/modular-form-renderer 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

zenify/modular-form-renderer

最新稳定版本:v0.0.2

Composer 安装命令:

composer require zenify/modular-form-renderer

包简介

README 文档

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.

统计信息

  • 总下载量: 368
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2015-10-15