rkr / forms
dev-master
2020-08-10 20:39 UTC
Requires
- php: >= 7.4
- ext-bcmath: *
- ext-json: *
- ext-mbstring: *
- slim/psr7: ^1.1
Requires (Dev)
- phpstan/phpstan: ^0.12.11
- phpunit/phpunit: ^9.0
- slim/slim: ^4.5
- twig/twig: ^3.0
- vimeo/psalm: ^3.12
This package is auto-updated.
Last update: 2024-09-11 05:42:47 UTC
README
介绍
现在有许多基于PHP的表单类。我在决定开发自己的方法之前,使用了很多,并访问了更多。因此,该项目基于一些经验,主要覆盖了我的需求。
安装
composer require rkr/forms
示例
首次渲染
我们首先必须创建一个结构,该结构将代表我们未来的表单,并随后转换输入的数据并进行验证
use Forms\Form; $section = new Form\Section('Section', ['xyz' => 123], new Form\Checkbox(['data', 'active'], 'Active'), new Form\Input(['data', 'name'], 'Name', ['required' => true, 'minlenght' => 5, 'maxlength' => 64]), new Form\Email(['data', 'email'], 'Email') );
然后我们可以从结构创建数据结构
$structure = $section->render([], false); print_r(json_encode($structure, JSON_PRETTY_PRINT));
{
"type": "section",
"elements": [{
"name": ["data", "active"],
"title": "Active",
"value": null,
"messages": [],
"attributes": [],
"type": "checkbox"
}, {
"name": ["data", "name"],
"title": "Name",
"value": null,
"messages": [],
"attributes": {
"required": true,
"minlenght": 5,
"maxlength": 64
},
"type": "input"
}, {
"name": ["data", "email"],
"title": "Email",
"value": null,
"messages": [],
"attributes": [
"required": true
],
"type": "email"
}],
"attributes": {"xyz": 123},
"title": "Section"
}
如果我们已经有了数据,那么这些数据将包含在生成的数据结构中。这就是第一步的全部内容。现在可以使用任何模板引擎生成输出。我在项目中主要使用PHP和Twig,但任何模板引擎都可以使用。
转换
说明如下...
验证和第二次渲染
说明如下...
表单的禅意
- 数据应该易于推理
- 构建表单应尽可能直观
- 表单应使用声明性数据和用户定义的模板构建
- 能够替换现有组件并构建自己的组件
- I18N和L10N至关重要
- 外部依赖尽可能少
- PHP代码必须静态清洁
待办事项
-
测试的基本组件
-
I18N和L10N的处理
-
使用示例
-
每个组件在初始化后不应通过调用
convert
、validate
或render
来更改内部状态。