thomasleconte/form-generator
Composer 安装命令:
composer require thomasleconte/form-generator
包简介
PHP form generator compatible with Doctrine
README 文档
README
composer require thomasleconte/form-generator
This is a library that make you able to generate a form. This library use reflection principle to generate the best result from a class name or an existing object.
This form generator is compatible with Doctrine ORM. In fact, if one of your property doesn't have a PHP type, but a Doctrine type annotation, it will take this one.
Moreover, in the case of a select list generation, you can fill the list with Doctrine by specifying class name, key attribute and attribute value.
Check this out !
Summary
Construction
Basic generation
Personalize inputs
Personalize form
Surround your inputs / form
Special inputs
Hydrate dynamically select field
Construction
$generator = new FormGenerator();
By default, constructor doesn't need arguments. But you can provide a current Doctrine EntityManager instance.
If you installed Doctrine but you did not provide EntityManager instance to generator, it will try to get it from
bootstrap.phpfile at root of your project. This file is generally provided by most of Doctrine tutorials online, and return an instance of Doctrine EntityManager.
For more informations, check this tutorial.
Basic generation
// generate(objectOrClassName, form action, fields options, form options) $generator->generate(User::class, "/register"); $generator->show();
Or with a current object :
$user = new User("Elon", "Musk", 50); $generator->generate($user, "/register"); $generator->show();
You can add attribute to a field after generated a form. Update is also available :
... $generator->generate($user, "/register"); ... $generator->addAttribute("firstname", "placeholder", "Your first name"); $generator->updateAttribute("firstname", "placeholder", "I don't know what to write here"); $generator->show();
Personalize your inputs
By default, all inputs just have an auto-generated name attribute. But you can give them all attributes that you want. Imagine you want to add a class to firstname input of our User class used before :
$user = new User("Elon", "Musk", 50); $generator->generate($user, "/register", [ "firstname" => [ "class" => "input-field" ] ]); $generator->show();
You can also decide to hide one of fields. By default, "id" field of your entity is hidden and you can't override it. You can hide others one with "hide" field option :
... $generator->generate($user, "/register", [ "age" => [ "hide" => true ] ]); ...
Personalize your form
Like inputs personalization, you can add all attributes that you want, just like that :
$user = new User("Elon", "Musk", 50); $generator->generate($user, "/register", [/* fields options */], [ "class" => "register-form" ]); $generator->show();
Surround your inputs / form
You can surround generation result of each inputs or form for give them div parent for example. When you use it, you must provide {{content}} which corresponding to input of form generation result. Check this out :
- Input surround
$user = new User("Elon", "Musk", 50); $generator->generate($user, "/register", [ "firstname" => [ "class" => "input-field", "surround" => "<div class='form-field'>{{content}}</div>" ], ]); $generator->show();
- Form surround
NB : For form surround, it surround inside of
<form>tags.
$user = new User("Elon", "Musk", 50); $generator->generate($user, "/register", [/* fields options */], [ "class" => "register-form", "surround" => "<div class='form-inputs'>{{content}}</div>" ]); $generator->show();
Special inputs
Sometimes you will want to use select or textarea tags inside your form. For use it, just precize type of input. For example, you want a textarea for firstname attribute and select list for age attribute of our User class. Check this out :
$user = new User("Elon", "Musk", 50); $generator->generate($user, "/register", [ "firstname" => [ "type" => "textarea" ], "age" => [ "type" => "select" ] ]); $generator->show();
Select field
When you make a select field, you must provide a list of items, with a value key for differentiate each of them, and a name key to display. Check this example :
$user = new User("Elon", "Musk", 50); $generator->generate($user, "/register", [ "firstname" => [ "type" => "textarea" ], "gender" => [ "type" => "select", "items" => [ array("value" => 1, "name" => "Man"), array("value" => 2, "name" => "Women" ), array("value" => 3, "name" => "Attack Helicopter") ] ] ]); $generator->show();
Moreover, you can fill items array with Doctrine. Instead of array, just give class name, and specify optionLabel / optionValue with attributes name of your class specified before. This is how to do :
$generator->generate($user, "/register", [ "firstname" => [ "type" => "textarea" ], "gender" => [ "type" => "select", "items" => Gender::class, "optionLabel" => "name", // name attribute of Gender class "optionValue" => "id" // id attribute of Gender class ] ]); $generator->show();
thomasleconte/form-generator 适用场景与选型建议
thomasleconte/form-generator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 03 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 thomasleconte/form-generator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 thomasleconte/form-generator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 9
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-03-23