abt / form
ZF2 / Doctrine 集成表单生成器
dev-master
2015-01-14 14:45 UTC
Requires
- php: >=5.3.23
- abt/common: dev-master
- zendframework/zend-form: >=2.2.7
- zendframework/zend-servicemanager: >=2.2.7
- zendframework/zend-validator: >=2.2.7
Requires (Dev)
- phpunit/phpunit: ~3.7
- squizlabs/php_codesniffer: 1.5.*
- zendframework/zendframework: >=2.2.7
This package is not auto-updated.
Last update: 2024-09-28 15:49:40 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 Form 的注解中,位于 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"}
* }
* }
* }
* }
这将按顺序将名称行放在默认字段集的第一位,将电子邮件行放在第二位。
类属性可以通过注解指定它们属于哪个字段集或行,通过名称映射它们。
/**
* @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.name
和user.email
) - template 用于渲染此元素的视图部分。