form-manager / form-manager
PHP-HTML 表单管理器
v6.1.2
2021-07-19 22:13 UTC
Requires
- php: ^7.1 || ^8.0
- symfony/validator: ^4.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.13
- nyholm/psr7: ^1.0
- oscarotero/php-cs-fixer-config: ^1.0
- phpunit/phpunit: ^7.0 || ^8.0 || ^9.0
- dev-master
- v6.1.2
- v6.1.1
- v6.1.0
- v6.0.1
- v6.0.0
- v5.x-dev
- v5.1.3
- v5.1.2
- v5.1.1
- v5.1.0
- v5.0.4
- v5.0.3
- v5.0.2
- v5.0.1
- v5.0
- v4.7.3
- v4.7.2
- v4.7.1
- 4.7.0
- v4.6.2
- v4.6.1
- v4.6.0
- v4.5.0
- v4.4.0
- v4.3.9
- v4.3.8
- v4.3.7
- v4.3.6
- v4.3.5
- v4.3.4
- v4.3.3
- v4.3.2
- v4.3.1
- v4.3
- v4.2
- v4.1
- v4.0.1
- v4.0
- v3.10.1
- v3.10
- v3.9.4
- v3.9.3
- v3.9.2
- v3.9.1
- v3.9.0
- v3.8.3
- v3.8.2
- v3.8.1
- v3.8.0
- v3.7.1
- v3.7.0
- v3.6.2
- v3.6.1
- v3.6.0
- v3.5.2
- v3.5.1
- v3.5.0
- v3.4.1
- v3.4.0
- v3.1
- v3.0
- v2.1
- v2.0
- v1.1
- v1.0
- v0.1
This package is auto-updated.
Last update: 2024-09-22 11:44:40 UTC
README
注意:这是 FormManager 6.x 的文档
对于 v5.x 版本 点击此处
安装
此软件包需要 PHP>=7.1
并可在 Packagist 上获取
composer require form-manager/form-manager
创建字段
FormManager 是命名空间的,但您只需将一个类导入到您的上下文中即可
use FormManager\Factory as F;
使用导入的工厂创建所有表单元素
//Create an input type="text" element $name = F::text(); //Create the input with a label $name = F::text('Please, introduce your name'); //Or with extra attributes $name = F::text('Please, introduce your name', ['class' => 'name-field']); //Add or remove attributes $name->setAttribute('title', 'This is the name input'); $name->removeAttribute('class'); $name->setAttributes([ 'required', 'readonly', 'tabindex' => 2, 'maxlength' => 50 ]); //Set the value $name->setValue('MyName'); //Use magic properties to get/set/remove attributes $name->class = 'name-field'; $name->required = false; unset($name->readonly);
所有可用输入列表
支持所有 HTML5 字段类型
F::checkbox($label, $attributes)
F::color($label, $attributes)
F::date($label, $attributes)
F::datetimeLocal($label, $attributes)
F::email($label, $attributes)
F::file($label, $attributes)
F::hidden($value, $attributes)
F::month($label, $attributes)
F::number($label, $attributes)
F::password($label, $attributes)
F::radio($label, $attributes)
F::range($label, $attributes)
F::search($label, $attributes)
F::select($label, $options, $attributes)
F::submit($label, $attributes)
F::tel($label, $attributes)
F::text($label, $attributes)
F::textarea($label, $attributes)
F::time($label, $attributes)
F::url($label, $attributes)
F::week($label, $attributes)
注意,所有输入除了
hidden
和select
之外都接受相同的参数。
验证
此库内部使用 symfony/validation 来执行基本的 html5 验证和错误报告。支持如 required
、maxlength
、minlength
、pattern
等HTML5验证属性,此外还支持分配给每个输入的内置验证,如 email、url、date 等。
$email = F::email(); $email->setValue('invalid-email'); //Validate the value if ($email->isValid()) { return true; } //Get errors $error = $email->getError(); //Print the first error message echo $error; //Iterate through all messages foreach ($error as $err) { echo $err->getMessage(); } //You can also customize/translate the error messages $email->setErrorMessages([ 'email' => 'The email is not valid', 'required' => 'The email is required', 'maxlength' => 'The email is too long, it must have {{ limit }} characters or less', ]); //And add more symfony validators $ip = F::text(); $ip->addConstraint(new Constraints\Ip());
请参阅 symfony 支持的所有约束
渲染 HTML
$name = F::text('What is your name?', ['name' => 'name']); echo $name;
<label for="id-input-1">What is your name?</label> <input id="id-input-1" type="text" name="name">
使用 {{ label }}
和 {{ input }}
占位符设置自定义模板
$name->setTemplate('{{ label }} <div class="input-content">{{ input }}</div>'); echo $name;
<label for="id-input-1">What is your name?</label> <div class="input-content"><input id="id-input-1" type="text" name="name"></div>
如果您想将之前的模板包裹在自定义 HTML 中,请使用 {{ template }}
占位符
$name->setTemplate('<fieldset>{{ template }}</fieldset>'); echo $name;
<fieldset><label for="id-input-1">What is your name?</label> <div class="input-content"><input id="id-input-1" type="text" name="name"></div></fieldset>
字段分组
将字段分组以遵循特定的数据结构
分组
分组允许将一组输入放置在特定的名称下
$group = F::group([ 'name' => F::text('Username'), 'email' => F::email('Email'), 'password' => F::password('Password'), ]); $group->setValue([ 'name' => 'oscar', 'email' => 'oom@oscarotero.com', 'password' => 'supersecret', ]);
单选按钮组
单选按钮的特例,其中所有输入都共享相同的名称,但具有不同的值
$radios = F::radioGroup([ 'red' => 'Red', 'blue' => 'Blue', 'green' => 'Green', ]); $radios->setValue('blue');
提交组
将多个提交按钮分组在同一名称下但具有不同值的特例
$buttons = F::submitGroup([ 'save' => 'Save the row', 'duplicate' => 'Save as new row', ]); $buttons->setName('action');
分组集合
是使用同一组值的值集合
$groupCollection = F::groupCollection( f::group([ 'name' => F::text('Name'), 'genre' => F::radioGroup([ 'm' => 'Male', 'f' => 'Female', 'o' => 'Other', ]), ]) ]); $groupCollection->setValue([ [ 'name' => 'Oscar', 'genre' => 'm' ],[ 'name' => 'Laura', 'genre' => 'f' ], ])
多组集合
是使用不同组的值集合,使用字段 type
来识别每行使用的组
$multipleGroupCollection = F::multipleGroupCollection( 'text' => f::group([ 'type' => F::hidden(), 'title' => F::text('Title'), 'text' => F::textarea('Body'), ]), 'image' => f::group([ 'type' => F::hidden(), 'file' => F::file('Image file'), 'alt' => F::text('Alt text'), 'text' => F::textarea('Caption'), ]), 'link' => f::group([ 'type' => F::hidden(), 'text' => F::text('Link text'), 'href' => F::url('Url'), 'target' => F::select([ '_blank' => 'New window', '_self' => 'The same window', ]), ]), ]); $multipleGroupCollection->setValue([ [ 'type' => 'text', 'title' => 'Welcome to my page', 'text' => 'I hope you like it', ],[ 'type' => 'image', 'file' => 'avatar.jpg', 'alt' => 'Image of mine', 'text' => 'This is my photo', ],[ 'type' => 'link', 'text' => 'Go to my webpage', 'href' => 'https://oscarotero.com', 'target' => '_self', ], ]);
Datalist
Datalist 也允许使用,只需使用 createDatalist()
方法即可
$input = F::search(); $datalist = $input->createDatalist([ 'female' => 'Female', 'male' => 'Male' ]); echo $input; echo $datalist;
表单
我们需要一个表单来将这些事物组合在一起。
$loginForm = F::form([ 'username' => F::text('User name'), 'password' => F::password('Password'), '' => F::submit('Login'), ]); $loginForm->setAttributes([ 'action' => 'login.php', 'method' => 'post', ]); //Load data from globals $_GET, $_POST, $_FILES $loginForm->loadFromGlobals(); //Load data passing the arrays $loginForm->loadFromArrays($_GET, $_POST, $_FILES); //Or load from PSR-7 server request $loginForm->loadFromServerRequest($serverRequest); //Get loaded data $data = $loginForm->getValue(); //Print the form echo $loginForm; //Access to specific inputs: echo $loginForm->getOpeningTag(); echo '<h2>Login:</h2>'; echo $loginForm['username']; echo '<hr>'; echo $loginForm['password']; echo '<hr>'; echo $loginForm['']; echo $loginForm->getClosingTag(); //Iterate with all inputs echo $loginForm->getOpeningTag(); echo '<h2>Login:</h2>'; foreach ($loginForm as $input) { echo "<div>{$input}</div>"; } echo $loginForm->getClosingTag();