lin3s/wp-symfony-form
Composer 安装命令:
composer require lin3s/wp-symfony-form
包简介
WordPress plugin to allow using Symfony form component with ease
README 文档
README
#WP Symfony Form
WordPress plugin to allow using Symfony form component with ease
Installation
If you are using composer run the following command:
$ composer require lin3s/wp-symfony-form
If your composer.json is properly set up you should find this package in plugins folder
Usage
First of all, enable this plugin in the WordPress admin panel.
To create your first form extend as usual from the AbstractType class provided by Symfony component.
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\EmailType; use Symfony\Component\Form\Extension\Core\Type\TextareaType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Validator\Constraints; class ContactType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('name', TextType::class, [ 'constraints' => new Constraints\NotBlank(), 'label' => 'Name', ]) ->add('surname', TextType::class, [ 'constraints' => new Constraints\NotBlank(), 'label' => 'Surname', ]) ->add('phone', TextType::class, [ 'constraints' => new Constraints\NotBlank(), 'label' => 'Phone', ]) ->add('email', EmailType::class, [ 'constraints' => new Constraints\Email(), 'label' => 'Email', ]) ->add('message', TextareaType::class, [ 'constraints' => new Constraints\NotBlank(), 'label' => 'Message', ]) ->add('conditions', CheckboxType::class, [ 'mapped' => false, ]); } }
To enable the Ajax calls for this form you need to subscribe to the wp_symfony_form_wrappers WordPress hook.
add_filter('wp_symfony_form_wrappers', function($formWrappers) { $formWrappers->add( new FormWrapper( 'contact', 'Fully/Qualified/Namespace/ContactType' ) ); });
###Rendering the form In case you want to use Twig for rendering a Bridge is provided, just run the following code line passing Twig instance
TwigBridge::addExtension($twig); // if you want to customize the form theme TwigBridge::addExtension($twig, 'component/form.twig');
component/form.twigis your custom form theme that will be used to render the forms. Check the docs about form customization for further info.
Timber
In case you are using Timber you should use twig_apply_filters hook.
Also, you have to load the form base views inside Timber global locations variable:
\Timber\Timber::$locations = [ (...) ABSPATH . '../vendor/symfony/twig-bridge/Resources/views/Form/', ];
Important Submit event is binded to every element with .form class. In case you need to change it just do the following:
WPSymfonyForm.formSelector = '.your-selector';
Also error container for each form item can be changed using WPSymfonyForm.formErrorsSelector.
###The FormWrapper
The FormWrapper is a class designed to contain a form and all its related actions. As you've seen above a new instance is
created for each form you want to use in your WordPress project, and need to be registered inside the
FormWrapperRegistry.
As first parameter it receives the fully qualified namespace and as second parameter it receives an array of classes
implementing Action interface.
###Actions on success
In case you need to perform any server side actions, it's as easy as to implement execute method of Action interface.
A form instance will to be used as desired. Check src/Action folder to check already implemented actions.
To bind this action to a specific form you need to add it in the FormWrapper.
For client side success actions you can add your callback using the global WPSymfonyForm namespace as follows:
WPSymfonyForm.onSuccess(function ($form) { if ($form.hasClass('form--contact')) { // ANYTHING YOU WANT TO DO } $form.find('.form__footer').html('<p>Form successfully submitted</p>'); });
onSuccess()andonError()are available to hook into the form.
lin3s/wp-symfony-form 适用场景与选型建议
lin3s/wp-symfony-form 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.06k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 11 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 lin3s/wp-symfony-form 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lin3s/wp-symfony-form 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.06k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-11-27