pugx/formable

PUGX Symfony 表单生成器

维护者

详细信息

github.com/PUGX/formable

源代码

问题

安装次数: 2,711

依赖项: 0

建议者: 0

安全性: 0

星星: 11

关注者: 11

分支: 4

公开问题: 0

类型:bundle

v0.2 2015-07-15 13:50 UTC

This package is auto-updated.

Last update: 2024-09-13 06:50:37 UTC


README

Build Latest Stable Version Total Downloads Latest Unstable Version License

为什么?

因为将数据从Web请求传输到领域模型最干净的方式是通过使用DTOs。对于简单的DTOs,Symfony要求您创建2个类,即FormType类和SomethingDTO类。

如何?

此Bundle允许您通过@Formable()注解来描述DTOs。让我们看看一个例子。

示例

数据传输对象

use Formable\Definition\Formable;
use Symfony\Component\Validator\Constraints as Assert;

class PublishPostCommand
{
    /**
     * @Formable(name="title", dataType="text")
     *
     * @Assert\Length(max=250)
     */
    public $title;

    /**
     * @Formable(name="content", dataType="text")
     */
    public $content;

    /**
     * @Formable(name="tags", dataType="collection", options={
     *   "type"="text",
     *   "allow_add"=true
     * })
     *
     * @Assert\Count(
     *   min = "2"
     * )
     *
     */
    public $tags;

    /**
     * @Formable(name="date", dataType="date", options={
     *   "widget"="single_text",
     *   "format"="yyyy-M-d"
     * })
     */
    public $date;
}

嵌入式DTOs

    /**
     * @var
     *
     * @Formable(name="moneyDTO", class="Formable\Tests\Integration\DTOs\TestMoneyDTO")
     */
    public $moneyDTO;

控制器

public function publishAction(Request $request)
{
    $publishCommand = new PublishPostCommand();
    $publishCommand->date = new \DateTime('now');
    
    $form = $this->get('pugx.formable')->generate($publishCommand);
    $form->submit($request->request->all(), false /* Do not clear missing data */);
    
    if ($form->isValid()) {
        ...
    }
}

注解详解

@Formable()注解遵循Symfony\Component\Form\FormBuilderInterface接口。

参数:

  • name: [字符串] 字段名称
  • dataType: [字符串] 表单类型
  • options: [数组] 表单类型选项
    /**
     * @Formable(name="date", dataType="date", options={
     *   "format"= "yyyy-MM-dd",
     *   "days" = {1,2,3,4}
     * })
     */
    public $date;

安装

composer require pugx/formable

// Register the Bundle

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            ...
            new \Formable\Bundle\FormableBundle(),
        );

        return $bundles;
    }

}

运行测试

bin/test