ZF2 / Doctrine 整合表单生成器

dev-master 2015-01-07 21:22 UTC

This package is not auto-updated.

Last update: 2024-09-24 03:19:49 UTC


README

关于

此 ZF2 模块旨在将 Doctrine 实体转换为 \Zend\Form\Form 对象,然后在页面上渲染它。

安装

  • 通过 composer 安装
  • 将以下内容添加到您的 composer.json 文件中:"fousheezy/form": "dev-master"
  • 将模块添加到您的 zend 项目中(从 ZF2 的根目录开始)编辑 config/application.config.php 并将 'FzyForm' 添加到 'modules' 数组

服务

  • FzyForm\Render 返回类型为 \FzyForm\Service\Render 的渲染服务

视图辅助函数

  • fzyForm 接受一个 \Zend\Form\Form 元素和一个可选的第二个参数来覆盖表单的选项。如果没有提供参数,它将返回一个 \FzyForm\View\Helper\FzyForm 对象

AutoForm

基本

关于您的表单如何输出的大多数配置都描述在 Zend 表单注解中的 Doctrine 实体本身内。所有配置都包含在表单注解的 "Options" 中

例如

use Doctrine\ORM\Mapping as ORM;
use Zend\Form\Annotation;


/**
 * Class User
 * @package Application\Entity\Base
 * @ORM\Entity
 * @ORM\Table(name="user")
 * @Annotation\Options({
 *      "autorender": {
 *          "ngModel": "user",
 *          "fieldsets": {
 *              {
 *                  "name": \FzyForm\Annotation\FieldSet::DEFAULT_NAME,
 *                  "legend": "Basic Info"
 *              }
 *          }
 *      }
 * })
 *
 */

此模块在 @Annotation\Options 的 "autorender" 键中查找配置数据

  • ngModel 定义了将容纳表单元素值的容器的名称,在 AngularJS 中。
  • fieldsets 定义了此表单包含的字段集数组。

一个字段集至少包含一个 "name" 值。您可以选择在字段集中定义行,如下所示

*      "autorender": {
 *          "ngModel": "user",
 *          "fieldsets": {
 *              {
 *                  "name": \FzyForm\Annotation\FieldSet::DEFAULT_NAME,
 *                  "legend": "Basic Info"
 *                  "rows": {
 *                     {"name": "nameRow"},
 *                     {"name": "emailRow"}
 *                  }
 *              }
 *          }
 *      }

这将按照默认字段集中先排序 "name" 行,然后是 "email" 行的顺序。

类属性可以被注解来指定它们属于哪个字段集或行,通过名称映射。

/**
     * @ORM\Column(type="string", length=255, name="name")
     *
     * @Annotation\Type("Zend\Form\Element\Text")
     * @Annotation\Attributes({
     *  "data-ng-disabled": "saving",
     *  "required": true
     * })
     * @Annotation\Options({
     *      "label":"Name",
     *      "autorender": {
     *          "ngModel": "name"
     *      }})
     * @Annotation\ErrorMessage("Please provide a name")
     *
     * @var string
     */
    protected $name;

    /**
     * @ORM\Column(type="string", length=255)
     * @Annotation\Type("Zend\Form\Element\Text")
     * @Annotation\Attributes({
     *  "data-ng-disabled": "saving"
     * })
     * @Annotation\Options({
     *      "label":"Email",
     *      "autorender": {
     *          "ngModel": "email"
     *      }})
     * @Annotation\AllowEmpty()
     * @Annotation\Validator({"name": "EmailAddress"})
     * @Annotation\ErrorMessage("Please provide an email")
     * @var string
     */
    protected $email;

类似地,在属性定义中,您可以在 Zend Forms 注解 \Options 的 docblock 中添加 "autorender" 选项来指定

  • ngModel 该表单元素将在 Angular Scope 中绑定到的属性(在此例中为 user.nameuser.email
  • template 要用于渲染此元素的视图部分。