solidworx/form-handler-bundle
Composer 安装命令:
composer require solidworx/form-handler-bundle
包简介
Easy form handling for Symfony forms
README 文档
README
The FormHandler component attempts to make controllers with basic form handlers cleaner by off-loading form handling to separate classes.
Table of Contents
Requirements
FormHandler requires PHP 7.1+ and Symfony 3.0+
Installation
Composer
$ composer require solidworx/form-handler-bundle:^1.0
Then register the bundle in your Symfony application:
<?php // app/AppKernel.php // ... public function registerBundles() { $bundles = [ // ... new SolidWorx\FormHandler\FormHandlerBundle(), ]; // ... )
Usage
A form can have a class that implements the FormHandlerInterface interface. This interface exposes a single method in which the form can be retrieved:
public function getForm(FormFactoryInterface $factory, Options $options);
This method can either return a standard form type, or use the factory to generate a form type.
<?php use Symfony\Component\Form\FormFactoryInterface; use SolidWorx\FormHandler\FormHandlerInterface; use SolidWorx\FormHandler\Options; class MyFormHandler implements FormHandlerInterface { public function getForm(FormFactoryInterface $factory, Options $options) { // either return MyFormType::class; // or return $factory->create(MyFormType::class); } }
The benefit of using the factory, is when you need to pass additional information or options to the form, E.G
return $factory->create(MyFormType::class, null, ['horizontal' => true]);
To register your form handler, register it as a service:
services: my.form.handler: class: MyFormHandler tags: - { name: 'form.handler' }
Inside your controller, use the form.handler service to handle your form:
<?php class MyController extends Controller { public function addAction() { return $this->get('solidworx.form_handler')->handle(MyFormHandler::class); // MyFormHandler will automatically be pulled from the container if it is tagges with `form.handler` } }
This will process the necessary logic on the form (submit the form and handle the request etc).
If you need to handle a failed form, you need to implement the FormHandlerFailInterface interface:
<?php use SolidWorx\FormHandler\FormRequest; use SolidWorx\FormHandler\FormHandlerInterface; use SolidWorx\FormHandler\FormHandlerFailInterface; use Symfony\Component\Form\FormErrorIterator; class MyFormHandler implements FormHandlerInterface, FormHandlerFailInterface { // ... public function onFail(FormRequest $formRequest, FormErrorIterator $errors, $data = null) { // Form submission has failed, probably due to a validation error. // Handle it here if you need specific custom logic } }
If you need to handle a successful form submission, implement the FormHandlerSuccessInterface interface:
<?php use SolidWorx\FormHandler\FormRequest; use SolidWorx\FormHandler\FormHandlerInterface; use SolidWorx\FormHandler\FormHandlerSuccessInterface; class MyFormHandler implements FormHandlerInterface, FormHandlerSuccessInterface { // ... public function onSuccess($data, FormRequest $form) { // $data is the submitted data from the form, do something with it here // This will probably save info to the DB } }
Adding options to a form
If you need to pass options to a form, you can add it as an array to the second argument of FormHandler::handle:
<?php class MyController extends Controller { public function addAction() { return $this->get('solidworx.form_handler')->handle(MyFormHandler::class, ['entity' => new Blog]); // MyFormHandler will automatically be pulled from the container if it is tagges with `form.handler` } }
The options will then be available in the getForm method as a Options object:
<?php use Symfony\Component\Form\FormFactoryInterface; use SolidWorx\FormHandler\FormHandlerInterface; use SolidWorx\FormHandler\Options; class MyFormHandler implements FormHandlerInterface { public function getForm(FormFactoryInterface $factory, Options $options) { return $factory->create(MyFormType::class, $options->get('entity')); } }
You can also configure the options to set what options is allowed, set default values, define required options etc. by implementing the FormHandlerOptionsResolver interface:
<?php use Symfony\Component\OptionsResolver\OptionsResolver; use SolidWorx\FormHandler\FormHandlerInterface; use SolidWorx\FormHandler\FormHandlerOptionsResolver; class MyFormHandler implements FormHandlerInterface, FormHandlerOptionsResolver { // ... public function configureOptions(OptionsResolver $resolver): void { $resolver->setRequired('entity'); $resolver->addAllowedTypes('entity', BlogEntity::class); $resolver->setDefault('another_options', 'myvalue'); } }
Advanced Usage
That is the very basics of the component. There are more advanced usages where you can customize the handling of a form to your specific needs.
Testing
To run the unit tests, execute the following command
$ vendor/bin/phpunit
Contributing
See CONTRIBUTING
License
FormHandler is open-sourced software licensed under the MIT license
Please see the LICENSE file for the full license.
solidworx/form-handler-bundle 适用场景与选型建议
solidworx/form-handler-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 65.42k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2017 年 03 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「form handling」 「symfony-bundle」 「symfony forms」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 solidworx/form-handler-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 solidworx/form-handler-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 solidworx/form-handler-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The bundle for easy using json-rpc api on your project
Diese Contao 4 Erweiterung stellt Google reCAPTCHA V2 in Form eines neuen Formularfeldes im Formulargenerator bereit. This extension provides Google reCAPTCHA V2 in the form of a new form field in the form generator of Contao Open Source CMS.
Provides form handler for Symfony form types
A Laravel Filament Forms slug field.
Bundle Symfony DaplosBundle
A jQuery augmented PHP library for creating and validating HTML forms
统计信息
- 总下载量: 65.42k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-03-22