定制 trt/formable 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

trt/formable

最新稳定版本:v0.1

Composer 安装命令:

composer require trt/formable

包简介

The Symfony Form Generator

README 文档

README

Formable Symfony Bundle

Latest Stable Version Total Downloads Latest Unstable Version License

Why?

Because the cleanest way to transfer data from a web request to the domain is by using DTOs. For simple DTOs Symfony forces you to create 2 classes, the FormType class and the SomethingDTO class.

How?

This Bundle allows you to describe DTOs by the annotation @Formable(). Let's see an example.

Example

The Data Transfer Object

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;
}

Embedded DTOs

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

The Controller

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

The annotation in depth

The @Formable() annotation follows the Symfony\Component\Form\FormBuilderInterface interface.

ARGUMENTS:

  • name: [string] the field name
  • dataType: [string] the FormType
  • options: [array] the FormType options
    /**
     * @Formable(name="date", dataType="date", options={
     *   "format"= IntlDateFormatter::MEDIUM,
     *   "days" = {1,2,3,4}
     * })
     */
    public $date;

Installation

composer require trt/formable

// Register the Bundle

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

        return $bundles;
    }

}

Run tests

bin/test

统计信息

  • 总下载量: 1.48k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 17
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 17
  • Watchers: 4
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-05-26