one234ru / html-input-generator
使用PHP生成单个HTML表单输入的代码。
Requires
- php: >=7.1
- one234ru/html-tag-generator: *
README
基于配置数组的HTML表单输入生成
此工具根据简单的配置生成各种网络表单字段的HTML源代码 - <input>
、<select>
和 <textarea>
。
该库基于 one234ru/html-tag-generator。
安装
composer require one234ru/html-input-generator
使用
要获取表单字段的HTML,需要创建字段对象,向其构造函数传递两个参数:字段配置和与字段对应的值。将对象转换为字符串将得到HTML。
以下示例生成了 <input type="text" name="something">
,其值从 $_GET
数组中提取。
$config = [ 'type' => 'text', 'name' => 'something', ]; $value = $_GET['something'] ?? ''; $input = new \One234ru\HTMLinputGenerator($config, $value); $html = strval($input);
几乎任何配置都包括参数 type
、name
和 attr
。
-
name
被设置为同名的属性值
(如果不存在,则标签将没有属性) -
attr
是键值对形式的任意属性列表
(包括class
、placeholder
和style
) -
type
- 定义将生成哪种类型的字段;
还会影响处理其他参数
type
可以包含以下值。
'text'
或空字符串
生成一个 <input type="text">
。将 value
属性设置为构造函数的第二个参数,并使用 htmlspecialchars()
处理。
$config = [ 'type' => 'text', 'name' => 'something', 'attr' => [ 'class' => 'some-class', 'placeholder' => 'Type "something" here' ] ]; $value = 'Text with "quotes" + <script>hackers!</script>';
结果(格式化以提高可读性)
<input type="text" name="something" value="Text with &quot;quotes&quot; + &lt;script&gt;hackers!&lt;/script&gt;" class="some-class" placeholder="Type &quot;something&quot; here" >
'text'
是 type
的默认值,如果它为空或不存在,则将应用。
'textarea'
生成一个 <textarea>
,其值用作内容。
$config = [ 'type' => 'textarea', 'name' => 'something', 'attr' => [ 'rows' => 5, 'style' => 'width: 100%; line-height: 1.25;' ] ]; $value = '"quoted" and <script>';
结果(格式化以提高可读性)
<textarea name="something" rows="5" style="width: 100%; line-height: 1.25;" >&quot;quoted&quot; and &lt;script&gt;</textarea>
'checkbox'
和 'radio'
这种变体生成相应类型的 <input>
。
可以指定 value
参数。如果它与传递给构造函数的值匹配,则字段的 checked
属性被打开
$cfg = [ 'type' => 'checkbox', 'name' => 'something', 'value' => 1, ]; $value = '1';
<input type="checkbox" name="something" value="1" checked>
比较是非严格的。在整数 0
和空字符串匹配的情况下,返回 false
。
'submit'
、'reset'
生成相应类型的 <input>
。
如果指定了 value
参数,它将传递到同名的属性。
构造函数的第二个参数没有效果。
$config = [ 'type' => 'submit', 'name' => 'something', 'value' => 'text on the button' ]; $value = 'whatever';
<input type="submit" name="something" value="text on the button">
'hidden'
可以生成带有来自HTTP查询的 value
属性的 <input type="hidden">
。
$config = [ 'name' => 'secret', 'type' => 'hidden', ]; $value = 'custom';
<input type="hidden" name="secret" value="custom">
如果 value
是显式的,则第二个参数将被忽略
$config = [ 'name' => 'secret', 'type' => 'hidden', 'value' => 'steady' ]; $value = 'custom'; // ignored
<input type="hidden" name="secret" value="steady">
'file'
生成 <input type="file">
。其他工作参数是 name
和 attr
。
'select'
生成 <select>
标签。
options
、optgroups
和 multiple
与标准配置参数结合。
options
参数
包含选项列表,任何选项都可以用两种方式声明
-
作为具有键
value
、text
和可选的attr
的数组。
value
成为同名的属性值,text
成为<option>
标签的内容。 -
作为键值对。
在这种情况下,键被视为value
,而值被视为text
。
如果选项的 value
与传递给构造函数的值匹配,则将其 selected
属性设置为
$config = [ 'type' => 'select', 'name' => 'something', 'options' => [ '' => '(choose)', 1 => 'One', 2 => 'Two', [ 'value' => 3, 'text' => 'Three', 'attr' => [ 'data-something' => 'Something', ] ] ] ]; $value = '3';
HTML(格式化以提高可读性)
<select name="something"> <option value="">(choose)</option> <option value="1">One</option> <option value="2">Two</option> <option value="3" data-something="Something" selected>Three</option> </select>
multiple
标志
启用此参数将设置同名属性,并影响两个主要方面
-
将
name
属性附加一对空括号 —[]
。
所以请不要自己放入[]
。 -
匹配选项值与构造函数传递的值的方式已更改:搜索数组而不是简单比较。
$config = [ 'type' => 'select', 'name' => 'something', 'multiple' => true, 'options' => [ '' => '(choose)', 1 => 'One', 2 => 'Two', 3 => 'Three', ] ]; $value = [ '1', '3' ];
<select multiple name="something[]"> <option value="">(choose)</option> <option value="1" selected>One</option> <option value="2">Two</option> <option value="3" selected>Three</option> </select>
optgroups
参数
此参数将选项分组到 <optgroup>
标签中。这些标签也可以有属性,尤其是 label
— 可见分组标题。
组和独立选项可以共存。
$config = [ 'type' => 'select', 'name' => 'something', 'options' => [ '' => '(choose)', ], 'optgroups' => [ [ 'attr' => [ 'label' => 'First group', ], 'options' => [ 1 => 'One', 2 => 'Two', ] ], [ 'attr' => [ 'label' => 'Second group', ], 'options' => [ 3 => 'Three', 4 => 'Four', ] ] ] ]; $value = '3';
<select name="something"> <option value="">(choose)</option> <optgroup label="First group"> <option value="1">One</option> <option value="2">Two</option> </optgroup> <optgroup label="Second group"> <option value="3" selected>Three</option> <option value="4">Four</option> </optgroup> </select>
任何其他 type
值
如果一个值不属于上述任何情况,将生成一个 <input>
标签,type
直接到标签的同名属性,而传递给构造函数的值到标签的 value
属性。
结果与 type='text'
的情况非常相似。
$config = [ 'type' => 'tel', 'name' => 'something', 'attr' => [ 'placeholder' => 'Enter your phone' ] ]; $value = '+74950000000';
<input type="tel" name="something" value="+74950000000" placeholder="Enter your phone" >