pugx / formable
PUGX Symfony 表单生成器
v0.2
2015-07-15 13:50 UTC
Requires
- php: >=5.4
- symfony/form: ^2.3
- symfony/framework-bundle: ^2.3
- symfony/validator: ^2.3
Requires (Dev)
- phpspec/phpspec: ^2.2
- phpunit/phpunit: ^4.6
This package is auto-updated.
Last update: 2024-09-13 06:50:37 UTC
README
为什么?
因为将数据从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