pollen-solutions/form 问题修复 & 功能扩展

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

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

pollen-solutions/form

Composer 安装命令:

composer require pollen-solutions/form

包简介

Pollen Solutions - Form Component - Generator and processor tools for PHP web application forms.

README 文档

README

Latest Stable Version MIT Licensed PHP Supported Versions

Pollen Solutions Form Component provides generator and processor tools for PHP web application forms.

Installation

composer require pollen-solutions/form

Basic Usage

Parameters definition

use Laminas\HttpHandlerRunner\Emitter\SapiEmitter;
use Pollen\Form\FormManager;

$forms = new FormManager();

$form = $forms->buildForm(
    [
        /**
         * Alias (Recommended)
         * {@internal Alias is used for generate default HTML tag class, title. 
         * By default an SHA-1 string is generated, it is not human readable.} 
         * @var string
         */
        'alias'  => 'auth',
        /**
         * Form fields
         * @see Configuration/Field] below for more details
         * @var array<string, array> 
         */
        'fields' => [
            'login' => [
                'type' => 'text',
            ],
            'pass'  => [
                'type' => 'password',
            ],
        ],
    ]
)->get();

if ($response = $form->handle()->proceed()) {
    (new SapiEmitter())->emit($response->psr());
    exit;
}

echo $form; 

Form instance definition

use Laminas\HttpHandlerRunner\Emitter\SapiEmitter;
use Pollen\Form\Form;
use Pollen\Form\FormManager;

$forms = new FormManager();

$form = $forms->buildForm(new class extends Form {
    protected ?string $alias = 'auth';

    public function defaultParams(): array
    {
        return array_merge(parent::defaultParams(), [
            'fields' => [
                'login' => [
                    'type' => 'text',
                ],
                'pass'  => [
                    'type' => 'password',
                ],
            ],
        ]);
    }
})->get();

if ($response = $form->handle()->proceed()) {
    (new SapiEmitter())->emit($response->psr());
    exit;
}

echo $form;

Dependency injection service definition

use Laminas\HttpHandlerRunner\Emitter\SapiEmitter;
use Pollen\Container\Container;
use Pollen\Form\Form;
use Pollen\Form\FormManager;

$container = new Container();

$forms = new FormManager([], $container);

class AuthForm extends Form
{
    /**
     * Alias identifier.
     * @var string|null
     */
    protected ?string $alias = null;
    
    /**
     * @inheritDoc
     */
    public function defaultParams(): array
    {
        return array_merge(parent::defaultParams(), [
            'fields' => [
                'login' => [
                    'type' => 'text',
                ],
                'pass'  => [
                    'type' => 'password',
                ],
            ],
        ]);
    }
}

$container->add(AuthForm::class);

$form = $forms->buildForm(AuthForm::class)->get();

if ($response = $form->handle()->proceed()) {
    (new SapiEmitter())->emit($response->psr());
    exit;
}

echo $form;

Configuration

Form

[
    /**
     * Action attribute in the form HTML tag.
     * @var string $action
     */
    'action'   => '',
    /**
     * List of enabled add-ons|List of enabled add-ons and their parameters.
     * @var string[]|array<string, array> $addons
     */
    'addons'   => [],
    /**
     * HTML content displayed after the form close tag.
     * @var string $after
     */
    'after'    => '',
    /**
     * List of the HTML attributes for the form tag.
     * @var array $attrs.
     */
    'attrs'    => [],
    /**
     * HTML content displayed before the form open tag.
     * @var string $before
     */
    'before'   => '',
    /**
     * List of enabled buttons|List of enabled buttons and their parameters.
     * @var array $buttons Liste des attributs des boutons actifs.
     */
    'buttons'  => [],
    /**
     * Enctype attribute in the form HTML tag.
     * @var string $enctype
     */
    'enctype'  => '',
    /**
     * List of observed and dispatched events.
     * @var array $events
     */
    'events'   => [],
    /**
     * List of field parameters.
     * @var array<string, array> $fields
     */
    'fields'   => [],
    /**
     * HTTP request method of form submission.
     * @var string $method
     */
    'method'   => 'post',
    /**
     * List of option parameters.
     * @var array $options
     */
    'options'  => [],
    /**
     * List of supported features.
     * @var string[] $supports
     */
    'supports' => ['session'],
    /**
     * Form title.
     * @var string $title
     */
    'title'    => $this->getAlias(),
    /**
     * CSRF token activation (recommended).
     * @var bool
     */
    'token'    => true,
    /**
     * List of parameters of the template view|View instance.
     * @var array|ViewInterface $view
     */
    'view'   => [],
    /**
     * List of HTML form wrapper parameters.
     * @var array $wrapper
     */
    'wrapper'  => []
];

Field

[
    /**
     * List of addon parameters.
     * @var array<string, array>|array $addons
     */
    'addons'      => [],
    /**
     * HTML content displayed after the field.
     * @var string $after
     */
    'after'       => '',
    /**
     * List of the HTML tag attributes.
     * @var array $attrs
     */
    'attrs'       => [],
    /**
     * HTML content displayed before the field.
     * @var string $before
     */
    'before'      => '',
    /**
     * List of choices for field with multiple value.
     * @var array $choices
     */
    'choices'     => [],
    /**
     * List of extra parameters.
     * @var array $extras
     */
    'extras'      => [],
    /**
     * Alias of the related group.
     * @var string|null $group
     */
    'group'       => null,
    /**
     * Field label.
     * {@internal true for default|false to hide|array of custom parameters}.
     * @var bool|string|array $label
     */
    'label'       => true,
    /**
     * Name HTML tag attribute in the handle HTTP request.
     * @var string $name Indice de qualification de la variable de requête.
     */
    'name'        => $this->getSlug(),
    /**
     * Display position.
     * @var int $position
     */
    'position'    => 0,
    /**
     * Required field parameters.
     * ---------------------------------------------------------------------------------------------------------
     * {@internal true for default parameters|false to disabling|array of custom parameters.
     * @var bool|string|array $required {
     *
     * HTML tag.
     * {@internal true for default parameters|false to hide|array of custom parameters based.}
     * @see \Pollen\Field\Drivers\RequiredDriver
     * @var bool|string|array $tagged
     *
     * Enable required validation.
     * @var bool $check
     *
     * Value of none value. empty string by default.
     * @var mixed $value_none
     *
     * Validation function name|validation callable.
     * @var string|callable $call
     *
     * List of arguments passed in the validation handle.
     * @var array $args
     *
     * Notification message if validation failed.
     * @type string $message
     * }
     */
    'required'    => false,
    /**
     * List of supported features.
     * @var array $supports label|wrapper|request|tabindex|transport.
     */
    'supports'    => [],
    /**
     * Title.
     * @var string|null $title
     */
    'title'       => null,
    /**
     * Disable the persistent value in the submission HTTP response.
     * {@internal if null, use the support feature value.}
     * @var bool|null $transport
     */
    'transport'   => null,
    /**
     * Field type.
     * @var string $type
     */
    'type'        => 'html',
    /**
     * List of validation tests.
     * ---------------------------------------------------------------------------------------------------------
     * @var string[]|array[][] $validations {
     *
     * Validation function name|validation callable.
     * @var string|callable $call
     *
     * List of arguments passed in the validation handle.
     * @var  array $args
     *
     * Notification message if validation failed.
     * @type string $message
     * }
     */
    'validations' => [],
    /**
     * Current value.
     * @var mixed $value
     */
    'value'       => null,
    /**
     * Field HTML Wrapper.
     * {@internal true for default parameters|false for hide|array of custom parameters.}
     * @var bool|string|array $wrapper
     */
    'wrapper'     => null
];

Stepwise process decomposition

use Laminas\HttpHandlerRunner\Emitter\SapiEmitter;
use Pollen\Form\FormManager;
use Pollen\Form\Exception\FieldMissingException;
use Pollen\Form\Exception\FieldValidateException;
use Pollen\Http\Request;

// Step 1 : Instantiate the Form Manager.
$forms = new FormManager();

// Step 2 : Register a Form.
$forms->registerForm(
    'auth',
    [
        'fields' => [
            'login' => [
                'type'     => 'text',
                'required' => true,
            ],
            'pass'  => [
                'type'        => 'password',
                'required'    => true,
                'validations' => 'password',
            ],
        ],
    ]
);

// Step 3 : Get and Boot the Form. After booting the form becomes immutable.
$form = $forms->get('auth')->boot();

// Step 4 : Form validation.
$request = Request::createFromGlobals();
$form->setHandleRequest($request);
$handle = $form->handle();

if ($form->isSubmitted()) {
    try {
        $field = $form->formField('login');
        $field->validate($handle->datas('login'));
    } catch (FieldMissingException $e) {
        $form->error($e->getMessage());
    } catch (FieldValidateException $e) {
        $form->error('Please enter a username.', ['field' => 'login']);
    }

    try {
        $field = $form->formField('pass');
        $field->validate($handle->datas('pass'));
    } catch (FieldMissingException $e) {
        $form->error($e->getMessage());
    } catch (FieldValidateException $e) {
        if ($e->isRequired()) {
            $form->error('Please enter a password.', ['field' => 'pass']);
        } elseif ($e->hasFlag('password')) {
            $form->error('Password format is invalid.', ['field' => 'pass']);
        }
    }

    if (!$form->handle()->isValidated()) {
        $form->handle()->fail();
    } else {
        $form->handle()->success();
    }
       
    /**
     * Redirect response (optionnal but best pratice).
     * {@internal Send form handle redirect response ensure that the form is always display through a GET HTTP method.
     * All request data is then catched and dispatched by the session processor.}
     */
    $response = $form->handle()->redirectResponse();

    if ($form->isSuccessful()) {
        (new SapiEmitter())->emit($response->psr());
    } else {
        (new SapiEmitter())->emit($response->psr());
    }
    exit;
    /** End of redirect response */
}

// Step 5 : Form render
echo $form;

pollen-solutions/form 适用场景与选型建议

pollen-solutions/form 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 08 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-08-18