stroker/form 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

stroker/form

Composer 安装命令:

composer require stroker/form

包简介

ZF2 module for extending forms with live clientside validation

README 文档

README

Build Status Coverage Status Scrutinizer Code Quality SensioLabsInsight Total Downloads HHVM Status

ZF2 module for extending forms with live clientside validation without need to write js validation code. You only need to define your validation rules server side with ZF2 and this module automaticaly adds the same rules with jQueryValidate. In case a client side version of the validation rule doesn't exist a fallback is done using ajax. For basic usage examples see the sandbox project StrokerFormSandbox.

BC Breaks since 1.0.0

  • Support for PHP versions lower than 5.6 is dropped.
  • StrokerForm\Renderer\JqueryValidate\Rule\RuleInterface has been extended with a new method canHandle. This method is called on a rule to check if it can process a certain validator. When you have some custom rules you'll need to update them to implement this new method.

BC Breaks since 0.1.0

For the new version you need to copy config/strokerform.global.php.dist to your projects config/autoload dir.

Installation

Installation of StrokerForm uses composer. For composer documentation, please refer to getcomposer.org.

  1. cd my/project/directory

  2. create or modify the composer.json file within your ZF2 application file with following contents:

    {
        "require": {
            "stroker/form": "*"
        }
    }
  3. install composer via curl -s https://getcomposer.org/installer | php (on windows, download https://getcomposer.org/installer and execute it with PHP). Then run php composer.phar install

  4. open my/project/directory/configs/application.config.php and add the following key to your modules:

    'StrokerForm',
  5. copy the file config/strokerform.global.php.dist from vendor\stroker\zf2-form to your projects config/autoload directory and rename it to strokerform.global.php.

  6. copy the assets to your public folder (my/project/directory/public).

Usage

First we need to make sure jquery is loaded by our application and the headScript() and inlineScript() view helpers are called. If you already have this in place you can skip this step.

<head>
  <?php echo $this->headLink() ?>
	<?php echo $this->headScript()->prependFile('//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js') ?>
</head>
<body>
<div class="container">
	<?php echo $this->content; ?>
</div>
<?php echo $this->inlineScript() ?>
</body>

For the ajax validation to work inputfilters needs to be hooked to the form. We need to create a serviceFactory and register it with a unique alias to the formManager (this is an pluginManager). If the inputFilters are already set to the form (i.e. in your form constructor) it's enough to register the form as an invokable

<?php
namespace MyProject\Service;

use Zend\ServiceManager\ServiceLocatorInterface;

class MyFormFactory implements \Zend\ServiceManager\FactoryInterface
{
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $form = new MyForm();
        $model = new MyModel();
        $form->setInputFilter($model->getInputFilter());
        return $form;
    }
}

Now let's add our new factory to the formManager.

<?php
return array(
    'stroker_form' => array(
        'forms' => array(
            'factories' => array(
                'my_form_alias' => 'MyProject\Service\MyFormFactory'
            )
        )
    )
);

Last thing we need to do is invoking the StrokerFormPrepare view helper where you are rendering your form. This view helper add all the needed javascripts to the headScript view helper

<?php
$this->strokerFormPrepare('my_form_alias');

// Do your normal form rendering here

Renderers

A renderer should implement the RendererInterface and is responsible for modifying the form rendering (setting inline javascript, modifying the form element attributes, view helpers etc.). Currently only the jqueryValidate renderer is available. Support for other validation libraries can be implemented as a seperate renderer.

JqueryValidate

Options

  • include_assets: Whether you want the view helper to include the needed assets or you like to do it yourself using a asset manager
  • use_twitter_bootstrap: Set this to true if you are using twitter bootstrap.
  • validate_options: Options for the jquery validate plugin. See jqueryValidate options for all possible options. i.e. if you also want to validate on keypress you can set onkeyup to true.
  • disable_ajax_fallback: Disables AJAX fallback for non available client side validators

Styling

If you are using twitter bootstrap and the recommended form structure the styling works out of the box. When you are using the ZF2 view helpers for your form you could style the input fields error and valid classes which are added on the fly by the jquery plugin.

Excluding elements from clientside validation

You can set the option strokerform-exclude on a form element

$name = new Element('name');
$name->setLabel('Your name');
$name->setOption('strokerform-exclude', true);

stroker/form 适用场景与选型建议

stroker/form 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 57.06k 次下载、GitHub Stars 达 39, 最近一次更新时间为 2012 年 11 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「form」 「module」 「validation」 「zend framework 2」 「jquery validate」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 stroker/form 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 stroker/form 我们能提供哪些服务?
定制开发 / 二次开发

基于 stroker/form 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 57.06k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 41
  • 点击次数: 23
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 39
  • Watchers: 5
  • Forks: 23
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2012-11-23