edouardkombo/multi-step-forms-bundle

轻松管理具有多步骤逻辑的 Symfony2 表单。

dev-master 2014-07-15 09:11 UTC

This package is not auto-updated.

Last update: 2024-09-24 01:51:17 UTC


README

关于

此 bundle 的功能是帮助您在一个控制器和一个视图中管理多步骤表单。此 bundle 为 Symfony > 2.5 制作。

要求

需要 PHP 版本 5.3 或更高。

安装

在 composer.json 中注册该 bundle

{
    "require": {
        "edouardkombo/multi-step-forms-bundle": "dev-master"
    }
}

现在,安装供应商

php composer.phar install

在您的 app/appKernel.php 中注册 MultiStepFormsBundle 命名空间

new EdouardKombo\MultiStepFormsBundle\EdouardKomboMultiStepFormsBundle(),

在您的 app/config/routing.yml 中添加 MultiStepFormsBundle 路由,我们将在后面进一步了解如何自定义路由

edouard_kombo_multi_step_forms:
resource: "@EdouardKomboMultiStepFormsBundle/Resources/config/routing.yml"
prefix:   /{_locale}/registration/step 

现在,在您的 app/config/config.yml 中添加配置参数。默认配置是为多步骤注册表单设计的,但您可以轻松扩展到任何您想要的表单类型。

edouard_kombo_multi_step_forms:
    multistep_forms:
        #Create your own param here, name it how you want, (user_registration) is just for demo
        user_registration:

            #Mandatory: Main entity where to save your forms datas
            entity_namespace: 'your_form_entity_namespace'
            
            #Mandatory: Form types in order of execution
            forms_order: ['namespace_of_first_form', 'namespace_of_second_form', 'namespace_of_third_form']
        
            #Mandatory: The three below form types will be redirected to a single action controller that will save datas
            #Of course, you can customize the way you want by overriding
            actions_order: ['edouard_kombo_multi_step_forms_create', 'edouard_kombo_multi_step_forms_create', 'edouard_kombo_multi_step_forms_create']
        
            #Mandatory: Each form will be render in a single view template specified in "indexAction", in the main controller
            #The last parameter is the route to be redirected to when the process is finished
            redirect_order: ['edouard_kombo_multi_step_forms_show', 'edouard_kombo_multi_step_forms_show', 'edouard_kombo_multi_step_forms_show', 'frontend_payment_choice']
            
            #Mandatory: This option is mandatory for routes
            allowed_roles: ['ROLE_USER', 'ROLE_MANAGER']
            
            #Optional: Form entity that will trigger authentication in case of user registration form
            authentication_trigger: 'form_type_namespace_that_triggers_authentication'
            
            #Optional: Your specified firewall (in security.yml), in case of user registration form
            authentication_firewall: 'main'
            
            #Optional: Where Doctrine will find the user, just after subsscription, for authentication
            authentication_entity_provider: 'VendorNameBundle:Entity'

            #Optional: Your mailer service, in case of user registration form
            #If not specified, no mail will be sent to user
            #authentication_mailer_service: 'your_mailer_servce'

文档

MultiStepFormsBundle 的所有魔法都在一个控制器、一个监听器和一个小帮手中编写。您只需要在您定义的全局路由中添加两个路由。

示例:您想设置一个多步骤注册表单。

  1. 在您的 userBundle 中创建一个 "UserStepRegistrationController.php" 控制器,并用它覆盖 EdouardKombo\MultiStepFormsBundle\Controller\MultiStepFormsController.php 类。请确保在您的 bundle 中针对单个模板视图。

  2. 在您的 userBundle 中创建一个 "LoginListener",并用它覆盖 EdouardKombo\MultiStepFormsBundle\Listener\LoginListener.php 类。

  3. 在您的 userBundle 中创建一个 "MultiStepFormsHelper",并用它覆盖 EdouardKombo\MultiStepFormsBundle\Helper\MultiStepFormsHelper.php 类。

  4. 覆盖您的服务,并注入在 app/config/config.yml 中指定的正确参数,这里为 "user registration"。

    parameters: multistep_forms.login_listener.class: VendorName\UserBundle\Listener\LoginListener multistep_forms.controller.class: VendorName\UserBundle\Controller\MultiStepFormsController multistep_forms.helper.class: VendorName\UserBundle\Helper\MultiStepFormsHelper

    services: multistep_forms.helper: class: %multistep_forms.helper.class% arguments: - %multistep_forms.CONFIG_PARAMETER% #Here it will be "user_registration" - @security.context - @service_container

     multistep_forms.login_listener:
         class: %multistep_forms.login_listener.class%
         arguments:
             - @doctrine.orm.entity_manager
             - @service_container
             - @security.context
    
     multistep_forms.controller:
         class: %multistep_forms.controller.class%     
    
  5. 定义您的路由。只需要两个路由,show 和 create。路由具有以下格式:/show/{user_role}/{step}

    • {user_role}: 您希望用户注册的角色(对于其他表单没有影响,但必填)。
    • {step}: 当前步骤,取决于配置

在 app/config/routing.yml 中定义您的全局路由

frontend_multistep_registration:
    resource: "@VendorUserBundle/Resources/config/routing/multistep_registration.xml"
    prefix:   /{_locale}/registration/step

在您的 multistep_registration.xml 中

<route id="frontend_multistep_registration_show" pattern="/show/{user_role}/{step}">
    <default key="_controller">VendorUserBundle:UserStepRegistration:index</default>
    <default key="user_role">manager</default>
    <requirement key="step">\d+</requirement>
    <requirement key="user_role">[a-zA-Z]+</requirement>         
</route> 

<route id="frontend_multistep_registration_create" pattern="/create/{user_role}/{step}">
    <default key="_controller">VendorUserBundle:UserStepRegistration:save</default>
    <default key="user_role">manager</default>
    <requirement key="_method">POST</requirement>
    <requirement key="step">\d+</requirement>
    <requirement key="user_role">[a-zA-Z]+</requirement>        
</route>        
  1. 在 app/config/config.yml 中更改 bundle 配置,然后您就可以开始了。

贡献

如果您想帮助我改进此 bundle,请确保它符合 PSR 编码标准。最简单的贡献方式是在仓库的 checkout 或您自己的 fork 上工作,而不是在安装的版本上。

问题

可以在 Github 问题跟踪器 中提交错误报告和功能请求。

如需更多信息,请直接联系我:edouard.kombo@gmail.com