ivangabriele/long-form-bundle

该软件包已被 废弃 并不再维护。未建议替代软件包。

Symfony 扩展包,可从简单的 Yaml 表单模型自动生成表单类型、实体和模板文件。

安装: 16

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 0

公开问题: 0

类型:symfony-bundle

v1.1.1 2016-02-01 23:55 UTC

This package is not auto-updated.

Last update: 2020-01-16 11:50:59 UTC


README

此扩展包提供了一种简单的方法,通过命令行界面(CLI)从简单的 Yaml 文件(表示表单模型)自动生成表单类。以下是可以自动生成的类

  • Symfony 表单类型类,
  • Twig 模板(可包含或渲染到您自己的模板中),
  • Doctrine ORM 实体类。

Build Status Code Coverage Scrutinizer Code Quality

SensioLabsInsight

安装

安装 IBLongFormBundle

将以下依赖项添加到您的 composer.json 文件中

composer.json

{
    "require": {
        "inspired-beings/long-form-bundle": "1.*"
    }
}

启用扩展包

并在内核中启用扩展包

app/AppKernel.php

<?php

public function registerBundles()
{
    $bundles = array(
        // ...
        new InspiredBeings\LongFormBundle\IBLongFormBundle(),
    );
}

如何使用它?

文件结构

以下是 IBLongFormBundle 基于的文件结构。同时,命名约定是自解释的。

.
├── src
    └── MyCompany
        └── MyBrand
            └── MyBundle
                ├── Entity
                |   └── StudentApplication.php (generated)
                ├── Form
                |   ├── Model
                |   |   └── StudentApplication.yml (form model to write)
                |   └── Type
                |       └── StudentApplicationType.php (generated)
                └── Ressources
                    └── views
                        └── Form
                            └── student_application.html.twig (generated)

编写表单模型

表单模型是一个基于 Symfony 表单类型参考 的简单 Yaml 文件

重要

  • 请注意不要将 defaults 关键字用作字段名称,因为它用于设置表单类型的通用选项。
  • 默认情况下,生成的表单字段类型是 字符串

Form/Model/StudentApplication.yml

# -------------------------------------------
# Form default options
# @see https://symfony.com.cn/doc/current/reference/forms/types/form.html

defaults:
    required: false

# -------------------------------------------
# Form fields
# @see https://symfony.com.cn/doc/current/reference/forms/types.html
# 
studentCivility:
    type: choice
    label: "Civility"
    required: true
    choices:
        'Mr': 'Mister'
        'Ms': 'Miss'
    expanded: true
studentFirstName:
    label: "First Name"
    required: true
studentLastName:
    label: "Last Name"
    required: true
studentBirthDate:
    label: "Birth Date"
    type: birthday
studentAddress:
    label: "Adress 1"
studentAddressBis:
    label: "Adress 2"
studentZipCode:
    label: "Zip Code"
studentCity:
    label: "City"
studentCountry:
    type: country
    label: "Country"
    data: "US"
studentEmail:
    type: email
    label: "Email"
    required: true
studentPhone:
    label: "Phone"

# -------------------------------------------
# Form buttons
# @see https://symfony.com.cn/doc/current/reference/forms/types.html#buttons
# 
reset:
    label: "RESET"
    type: reset
    attr: { class: 'button-reset' }
save:
    label: "SEND"
    type: submit
    attr: { class: 'button-submit' }

生成文件

打开您最喜欢的命令行界面并进入您的 Symfony 项目

php app/console generate:form MyBundleName:StudentApplication

这将生成文件结构上方标记为 (generated) 的所有文件

  • Entity/StudentApplication.php
  • Form/Type/StudentApplicationType.php
  • Ressources/views/Form/student_application.html.twig

如果您 不希望生成 Doctrine 实体,可以添加选项 --no-entity

php app/console generate:form MyBundleName:StudentApplication --no-entity

如果您想 自动更新数据库(调用 doctrine:schema:update --force),可以添加选项 --schema-update

php app/console generate:form MyBundleName:StudentApplication --schema-update