CraueFormFlowBundle 技术文档
2024-12-25 03:25:24作者:霍妲思
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。
登录后查看全文 
热门项目推荐
 PaddleOCR-VLPaddleOCR-VL 是一款顶尖且资源高效的文档解析专用模型。其核心组件为 PaddleOCR-VL-0.9B,这是一款精简却功能强大的视觉语言模型(VLM)。该模型融合了 NaViT 风格的动态分辨率视觉编码器与 ERNIE-4.5-0.3B 语言模型,可实现精准的元素识别。Python00 PaddleOCR-VLPaddleOCR-VL 是一款顶尖且资源高效的文档解析专用模型。其核心组件为 PaddleOCR-VL-0.9B,这是一款精简却功能强大的视觉语言模型(VLM)。该模型融合了 NaViT 风格的动态分辨率视觉编码器与 ERNIE-4.5-0.3B 语言模型,可实现精准的元素识别。Python00
- DDeepSeek-OCRDeepSeek-OCR是一款以大语言模型为核心的开源工具,从LLM视角出发,探索视觉文本压缩的极限。Python00
 MiniCPM-V-4_5MiniCPM-V 4.5 是 MiniCPM-V 系列中最新且功能最强的模型。该模型基于 Qwen3-8B 和 SigLIP2-400M 构建,总参数量为 80 亿。与之前的 MiniCPM-V 和 MiniCPM-o 模型相比,它在性能上有显著提升,并引入了新的实用功能Python00 MiniCPM-V-4_5MiniCPM-V 4.5 是 MiniCPM-V 系列中最新且功能最强的模型。该模型基于 Qwen3-8B 和 SigLIP2-400M 构建,总参数量为 80 亿。与之前的 MiniCPM-V 和 MiniCPM-o 模型相比,它在性能上有显著提升,并引入了新的实用功能Python00
 HunyuanWorld-Mirror混元3D世界重建模型,支持多模态先验注入和多任务统一输出Python00 HunyuanWorld-Mirror混元3D世界重建模型,支持多模态先验注入和多任务统一输出Python00
 AI内容魔方AI内容专区,汇集全球AI开源项目,集结模块、可组合的内容,致力于分享、交流。03 AI内容魔方AI内容专区,汇集全球AI开源项目,集结模块、可组合的内容,致力于分享、交流。03
 Spark-Scilit-X1-13B科大讯飞Spark Scilit-X1-13B基于最新一代科大讯飞基础模型,并针对源自科学文献的多项核心任务进行了训练。作为一款专为学术研究场景打造的大型语言模型,它在论文辅助阅读、学术翻译、英语润色和评论生成等方面均表现出色,旨在为研究人员、教师和学生提供高效、精准的智能辅助。Python00 Spark-Scilit-X1-13B科大讯飞Spark Scilit-X1-13B基于最新一代科大讯飞基础模型,并针对源自科学文献的多项核心任务进行了训练。作为一款专为学术研究场景打造的大型语言模型,它在论文辅助阅读、学术翻译、英语润色和评论生成等方面均表现出色,旨在为研究人员、教师和学生提供高效、精准的智能辅助。Python00
 GOT-OCR-2.0-hf阶跃星辰StepFun推出的GOT-OCR-2.0-hf是一款强大的多语言OCR开源模型,支持从普通文档到复杂场景的文字识别。它能精准处理表格、图表、数学公式、几何图形甚至乐谱等特殊内容,输出结果可通过第三方工具渲染成多种格式。模型支持1024×1024高分辨率输入,具备多页批量处理、动态分块识别和交互式区域选择等创新功能,用户可通过坐标或颜色指定识别区域。基于Apache 2.0协议开源,提供Hugging Face演示和完整代码,适用于学术研究到工业应用的广泛场景,为OCR领域带来突破性解决方案。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).Dockerfile014
 Spark-Chemistry-X1-13B科大讯飞星火化学-X1-13B (iFLYTEK Spark Chemistry-X1-13B) 是一款专为化学领域优化的大语言模型。它由星火-X1 (Spark-X1) 基础模型微调而来,在化学知识问答、分子性质预测、化学名称转换和科学推理方面展现出强大的能力,同时保持了强大的通用语言理解与生成能力。Python00 Spark-Chemistry-X1-13B科大讯飞星火化学-X1-13B (iFLYTEK Spark Chemistry-X1-13B) 是一款专为化学领域优化的大语言模型。它由星火-X1 (Spark-X1) 基础模型微调而来,在化学知识问答、分子性质预测、化学名称转换和科学推理方面展现出强大的能力,同时保持了强大的通用语言理解与生成能力。Python00
- PpathwayPathway is an open framework for high-throughput and low-latency real-time data processing.Python00
项目优选
收起
 docs
docsOpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
265
2.53 K
 kernel
kerneldeepin linux kernel
C
24
6
 pytorch
pytorchAscend Extension for PyTorch
Python
98
125
 ops-math
ops-math本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
598
151
 flutter_flutter
flutter_flutter暂无简介
Dart
555
124
 ohos_react_native
ohos_react_nativeReact Native鸿蒙化仓库
JavaScript
220
301
 cangjie_compiler
cangjie_compiler仓颉编译器源码及 cjdb 调试工具。
C++
117
93
 RuoYi-Vue3
RuoYi-Vue3🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.02 K
602
 cangjie_test
cangjie_test仓颉编程语言测试用例。
Cangjie
34
84
 Cangjie-Examples
Cangjie-Examples本仓将收集和展示高质量的仓颉示例代码,欢迎大家投稿,让全世界看到您的妙趣设计,也让更多人通过您的编码理解和喜爱仓颉语言。
Cangjie
357
1.83 K