martin1982/mfconditionalfieldsbundle
Composer 安装命令:
composer require martin1982/mfconditionalfieldsbundle
包简介
Symfony MF Conditional Fields bundle
README 文档
README
Make sure you load the mf-conditional fields JS library in your
project on the pages where you use conditional fields. You can either use script or module, currently
the script is executed when mfConditionalFields is available in the global namespace.
If you use a bundler (like Webpack), make sure to implement form initialization.
Example;
import mfConditionalFields from "mf-conditional-fields"; let initialized = []; document.addEventListener('DOMContentLoaded', () => { document.querySelectorAll('[data-conditional-rules]').forEach((element) => { let form = element.closest('form'); let formId = form.getAttribute('id'); if (initialized.includes(formId)) { return; } initialized.push(formId); mfConditionalFields('#' + formId, { debug: process.env.NODE_ENV === 'development', }); }); });
Installation
Make sure Composer is installed globally, as explained in the installation chapter of the Composer documentation.
Applications that use Symfony Flex
Open a command console, enter your project directory and execute:
composer require martin1982/mfconditionalfieldsbundle
Applications that don't use Symfony Flex
Step 1: Download the Bundle
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
composer require martin1982/mfconditionalfieldsbundle
Step 2: Enable the Bundle
Then, enable the bundle by adding it to the list of registered bundles
in the config/bundles.php file of your project:
// config/bundles.php return [ // ... Martin1982\MfConditionalFieldsBundle\MfConditionalFieldsBundle::class => ['all' => true], ];
Step 3: Enable the form theme
When using Twig you can initialize a form with conditional fields using a form_theme setting in your twig config:
twig: form_themes: ['@MfConditionalFields/conditional_field.html.twig']
Usage
On your FormType class implement the ConditionalRulesInterface for easy access to all options. When adding a field
using the FormBuilder you can make a field dependent by providing the conditional_options option.
The following options are available:
| Name | Type | Description |
|---|---|---|
| container | String | The container for the given action |
| action | String | The action that needs to be performed when the rules apply |
| logic | String | OR when only one condition needs to be met, AND when all need to be met |
| rules | Array | Array of rules to check, with at least 1 rule |
The rules consist of these options:
| Name | Type | Description |
|---|---|---|
| name | String | Field name to check |
| operator | String | Operator used to check field |
| value | String | Expected value for the rule to apply |
Example
In this example the code from the Symfony documentation is used to select if someone is attending. In addition, it'll show a reason text element when a user selects 'Maybe' as an option.
<?php declare(strict_types=1); namespace App\Form; use Martin1982\MfConditionalFieldsBundle\Rules\ConditionalRulesInterface; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; class AttendType extends AbstractType implements ConditionalRulesInterface { public function buildForm(FormBuilderInterface $builder, array $options): void { $builder ->add('isAttending', ChoiceType::class, [ 'choices' => [ 'Maybe' => 2, 'Yes' => 1, 'No' => 0, ], ]) ->add('reason', TextType::class, [ 'conditional_options' => [ 'container' => 'reason-container', 'action' => self::ACTION_SHOW, 'logic' => self::LOGIC_OR, 'rules' => [ [ 'name' => 'isAttending', 'operator' => self::OPERATOR_IS, 'value' => '2', ], ], ], ]) ; } }
Future releases / Contribute
This bundle includes a basic implementation. If you would like to contribute all options of the mf-conditional-fields bundle can be added.
Special thanks
Special thanks to Ali Khallad for creating this JavaScript library.
martin1982/mfconditionalfieldsbundle 适用场景与选型建议
martin1982/mfconditionalfieldsbundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.72k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 07 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 martin1982/mfconditionalfieldsbundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 martin1982/mfconditionalfieldsbundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.72k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-07-19