CraueFormFlowBundle 技术文档
2024-12-25 10:39:07作者:霍妲思
1. 安装指南
1.1 获取 Bundle
使用 Composer 下载并安装 CraueFormFlowBundle:
composer require craue/formflow-bundle
1.2 启用 Bundle
如果你没有使用 Symfony Flex,需要手动注册 Bundle:
// 在 config/bundles.php 中
return [
// ...
Craue\FormFlowBundle\CraueFormFlowBundle::class => ['all' => true],
];
对于 Symfony 3.4:
// 在 app/AppKernel.php 中
public function registerBundles() {
$bundles = [
// ...
new Craue\FormFlowBundle\CraueFormFlowBundle(),
];
// ...
}
2. 项目使用说明
CraueFormFlowBundle 提供了一个在 Symfony 项目中构建和处理多步骤表单的工具。以下是一些主要功能:
- 导航:支持下一步、上一步、重新开始。
- 步骤标签:为每个步骤设置标签。
- 跳过步骤:根据条件跳过某些步骤。
- 验证组:为每个步骤设置不同的验证组。
- 文件上传处理:支持文件上传。
- 动态步骤导航:可选的动态步骤导航。
- 提交后重定向:支持 Post/Redirect/Get 模式。
3. 项目 API 使用文档
3.1 创建一个多步骤表单
方法 A:整个流程使用一个表单类型
创建 Flow 类
// src/MyCompany/MyBundle/Form/CreateVehicleFlow.php
use Craue\FormFlowBundle\Form\FormFlow;
use Craue\FormFlowBundle\Form\FormFlowInterface;
use MyCompany\MyBundle\Form\CreateVehicleForm;
class CreateVehicleFlow extends FormFlow {
protected function loadStepsConfig() {
return [
[
'label' => 'wheels',
'form_type' => CreateVehicleForm::class,
],
[
'label' => 'engine',
'form_type' => CreateVehicleForm::class,
'skip' => function($estimatedCurrentStepNumber, FormFlowInterface $flow) {
return $estimatedCurrentStepNumber > 1 && !$flow->getFormData()->canHaveEngine();
},
],
[
'label' => 'confirmation',
],
];
}
}
创建表单类型类
// src/MyCompany/MyBundle/Form/CreateVehicleForm.php
use MyCompany\MyBundle\Form\Type\VehicleEngineType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
class CreateVehicleForm extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
switch ($options['flow_step']) {
case 1:
$validValues = [2, 4];
$builder->add('numberOfWheels', ChoiceType::class, [
'choices' => array_combine($validValues, $validValues),
'placeholder' => '',
]);
break;
case 2:
$builder->add('engine', VehicleEngineType::class, [
'placeholder' => '',
]);
break;
}
}
public function getBlockPrefix() {
return 'createVehicle';
}
}
方法 B:每个步骤使用一个表单类型
创建 Flow 类
// src/MyCompany/MyBundle/Form/CreateVehicleFlow.php
use Craue\FormFlowBundle\Form\FormFlow;
use Craue\FormFlowBundle\Form\FormFlowInterface;
use MyCompany\MyBundle\Form\CreateVehicleStep1Form;
use MyCompany\MyBundle\Form\CreateVehicleStep2Form;
class CreateVehicleFlow extends FormFlow {
protected function loadStepsConfig() {
return [
[
'label' => 'wheels',
'form_type' => CreateVehicleStep1Form::class,
],
[
'label' => 'engine',
'form_type' => CreateVehicleStep2Form::class,
'skip' => function($estimatedCurrentStepNumber, FormFlowInterface $flow) {
return $estimatedCurrentStepNumber > 1 && !$flow->getFormData()->canHaveEngine();
},
],
[
'label' => 'confirmation',
],
];
}
}
创建表单类型类
// src/MyCompany/MyBundle/Form/CreateVehicleStep1Form.php
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
class CreateVehicleStep1Form extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$validValues = [2, 4];
$builder->add('numberOfWheels', ChoiceType::class, [
'choices' => array_combine($validValues, $validValues),
'placeholder' => '',
]);
}
public function getBlockPrefix() {
return 'createVehicleStep1';
}
}
// src/MyCompany/MyBundle/Form/CreateVehicleStep2Form.php
use MyCompany\MyBundle\Form\Type\VehicleEngineType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class CreateVehicleStep2Form extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('engine', VehicleEngineType::class, [
'placeholder' => '',
]);
}
public function getBlockPrefix() {
return 'createVehicleStep2';
}
}
3.2 注册 Flow 为服务
XML 配置:
<services>
<service id="myCompany.form.flow.createVehicle"
class="MyCompany\MyBundle\Form\CreateVehicleFlow"
autoconfigure="true">
</service>
</services>
YAML 配置:
services:
myCompany.form.flow.createVehicle:
class: MyCompany\MyBundle\Form\CreateVehicleFlow
autoconfigure: true
3.3 创建表单模板
{# in src/MyCompany/MyBundle/Resources/views/Vehicle/createVehicle.html.twig #}
<div>
Steps:
{% include '@CraueFormFlow/FormFlow/stepList.html.twig' %}
</div>
{{ form_start(form) }}
{{ form_errors(form) }}
{% if flow.getCurrentStepNumber() == 1 %}
<div>
When selecting four wheels you have to choose the engine in the next step.<br />
{{ form_row(form.numberOfWheels) }}
</div>
{% endif %}
{{ form_rest(form) }}
{% include '@CraueFormFlow/FormFlow/buttons.html.twig' %}
{{ form_end(form) }}
3.4 创建 Action
// in src/MyCompany/MyBundle/Controller/VehicleController.php
public function createVehicleAction() {
$formData = new Vehicle(); // Your form data class. Has to be an object, won't work properly with an array.
$flow = $this->get('myCompany.form.flow.createVehicle'); // must match the flow's service id
$flow->bind($formData);
// form of the current step
$form = $flow->createForm();
if ($flow->isValid($form)) {
$flow->saveCurrentStepData($form);
if ($flow->nextStep()) {
// form for the next step
$form = $flow->createForm();
} else {
// flow finished
$em = $this->getDoctrine()->getManager();
$em->persist($formData);
$em->flush();
$flow->reset(); // remove step data from the session
return $this->redirectToRoute('home'); // redirect when done
}
}
return $this->render('@MyCompanyMy/Vehicle/createVehicle.html.twig', [
'form' => $form->createView(),
'flow' => $flow,
]);
}
4. 项目安装方式
通过 Composer 安装 CraueFormFlowBundle:
composer require craue/formflow-bundle
然后根据 Symfony 版本手动或自动注册 Bundle。
登录后查看全文
热门项目推荐
- QQwen3-Next-80B-A3B-InstructQwen3-Next-80B-A3B-Instruct 是一款支持超长上下文(最高 256K tokens)、具备高效推理与卓越性能的指令微调大模型00
- QQwen3-Next-80B-A3B-ThinkingQwen3-Next-80B-A3B-Thinking 在复杂推理和强化学习任务中超越 30B–32B 同类模型,并在多项基准测试中优于 Gemini-2.5-Flash-Thinking00
GitCode-文心大模型-智源研究院AI应用开发大赛
GitCode&文心大模型&智源研究院强强联合,发起的AI应用开发大赛;总奖池8W,单人最高可得价值3W奖励。快来参加吧~0107DuiLib_Ultimate
DuiLib_Ultimate是duilib库的增强拓展版,库修复了大量用户在开发使用中反馈的Bug,新增了更加贴近产品开发需求的功能,并持续维护更新。C++03GitCode百大开源项目
GitCode百大计划旨在表彰GitCode平台上积极推动项目社区化,拥有广泛影响力的G-Star项目,入选项目不仅代表了GitCode开源生态的蓬勃发展,也反映了当下开源行业的发展趋势。08- HHunyuan-MT-7B腾讯混元翻译模型主要支持33种语言间的互译,包括中国五种少数民族语言。00
GOT-OCR-2.0-hf
阶跃星辰StepFun推出的GOT-OCR-2.0-hf是一款强大的多语言OCR开源模型,支持从普通文档到复杂场景的文字识别。它能精准处理表格、图表、数学公式、几何图形甚至乐谱等特殊内容,输出结果可通过第三方工具渲染成多种格式。模型支持1024×1024高分辨率输入,具备多页批量处理、动态分块识别和交互式区域选择等创新功能,用户可通过坐标或颜色指定识别区域。基于Apache 2.0协议开源,提供Hugging Face演示和完整代码,适用于学术研究到工业应用的广泛场景,为OCR领域带来突破性解决方案。00- HHowToCook程序员在家做饭方法指南。Programmer's guide about how to cook at home (Chinese only).Dockerfile03
- PpathwayPathway is an open framework for high-throughput and low-latency real-time data processing.Python00
- Dd2l-zh《动手学深度学习》:面向中文读者、能运行、可讨论。中英文版被70多个国家的500多所大学用于教学。Python011
热门内容推荐
1 freeCodeCamp猫照片应用教程中的HTML注释测试问题分析2 freeCodeCamp全栈开发课程中测验游戏项目的参数顺序问题解析3 freeCodeCamp英语课程填空题提示缺失问题分析4 freeCodeCamp音乐播放器项目中的函数调用问题解析5 freeCodeCamp论坛排行榜项目中的错误日志规范要求6 freeCodeCamp 课程中关于角色与职责描述的语法优化建议 7 freeCodeCamp全栈开发课程中React组件导出方式的衔接问题分析8 freeCodeCamp Cafe Menu项目中link元素的void特性解析9 freeCodeCamp全栈开发课程中React实验项目的分类修正10 freeCodeCamp英语课程视频测验选项与提示不匹配问题分析
最新内容推荐
OMNeT++中文使用手册:网络仿真的终极指南与实用教程 基于Matlab的等几何分析IGA软件包:工程计算与几何建模的完美融合 PADS元器件位号居中脚本:提升PCB设计效率的自动化利器 电脑PC网易云音乐免安装皮肤插件使用指南:个性化音乐播放体验 Python Django图书借阅管理系统:高效智能的图书馆管理解决方案 Python开发者的macOS终极指南:VSCode安装配置全攻略 WebVideoDownloader:高效网页视频抓取工具全面使用指南 ReportMachine.v7.0D5-XE10:Delphi报表生成利器深度解析与实战指南 PhysioNet医学研究数据库:临床数据分析与生物信号处理的权威资源指南 海康威视DS-7800N-K1固件升级包全面解析:提升安防设备性能的关键资源
项目优选
收起

本仓将收集和展示高质量的仓颉示例代码,欢迎大家投稿,让全世界看到您的妙趣设计,也让更多人通过您的编码理解和喜爱仓颉语言。
Cangjie
338
1.19 K

🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
898
534

React Native鸿蒙化仓库
C++
188
265

deepin linux kernel
C
22
6

openGauss kernel ~ openGauss is an open source relational database management system
C++
140
188

旨在打造算法先进、性能卓越、高效敏捷、安全可靠的密码套件,通过轻量级、可剪裁的软件技术架构满足各行业不同场景的多样化要求,让密码技术应用更简单,同时探索后量子等先进算法创新实践,构建密码前沿技术底座!
C
374
387

为仓颉编程语言开发者打造活跃、开放、高质量的社区环境
Markdown
1.09 K
0

一款跨平台的 Markdown AI 笔记软件,致力于使用 AI 建立记录和写作的桥梁。
TSX
86
4

Nop Platform 2.0是基于可逆计算理论实现的采用面向语言编程范式的新一代低代码开发平台,包含基于全新原理从零开始研发的GraphQL引擎、ORM引擎、工作流引擎、报表引擎、规则引擎、批处理引引擎等完整设计。nop-entropy是它的后端部分,采用java语言实现,可选择集成Spring框架或者Quarkus框架。中小企业可以免费商用
Java
7
0

方舟分析器:面向ArkTS语言的静态程序分析框架
TypeScript
114
45