valksystems / bootbuilder
基于OOP PHP的简单表单构建器。您可以使用PHP代码从面向对象的表单构建中生成表单。
Requires
- php: >=5.4.0
Requires (Dev)
- php: >=5.4.0
- phpunit/phpunit: 4.5.1
This package is not auto-updated.
Last update: 2024-09-14 17:00:51 UTC
README
BootBuilder
在PHP中轻松构建Bootstrap表单。
安装
您可以使用Composer安装此包,请访问https://getcomposer.org.cn了解Composer
在composer.json中添加以下内容
"valksystems/bootbuilder": ">=1.0.0"
或者执行以下composer命令
composer require 'valksystems/bootbuilder:1*'
用法
查看示例文件夹,其中包含带有控件和不同样式的表单的示例
表单
首先,您必须创建一个表单对象。目前有两种选项。一个普通表单和一个水平表单(有关更多信息,请参阅Bootstrap页面)。
创建表单使用
$form = BootBuilder::open(); // Horizontal Form $horizontal = BootBuilder::openHorizontal();
在表单对象上,您可以使用以下方法设置普通表单的属性
setAction($action) // Set the action attribute setClass($class) // set class (this will replace current class) setId($id) // set the id of the form setMethod($method) // set the method, either get or post
要向表单添加控件或面板,使用
$form->add($control); // Or when adding multiple controls/panes at the same time: $form->addAll($pane1, $control, $submit);
HorizontalForm
在水平表单上,您可以设置两个额外的网格布局参数。要设置布局参数,请使用以下方法
$horizontal->setLabelCol($label_col) // for example col-md-3 $horizontal->setControlCol($control_col) // for example col-md-9
渲染
要渲染表单,只需调用render()
。您也可以使用render(true)
获取渲染的HTML。
控件
您可以使用以下控件,完全基于Bootstrap样式
- 按钮
- 提交(按钮)
- 复选框
- 单选按钮
- 文本(input类型=text)
- 数字(input类型=number)
- 电子邮件(input类型=email)
- 文本区域
- 隐藏(input类型=hidden)
- 密码(input类型=password)
- 下拉列表
控件默认操作
要创建一个新控件,使用以下方式构建控件的类
use \bootbuilder\Controls\Text; $text = new Text("name"); // Or you can directly give it a label, ID and value with the optional constructor parameters $text = new Text("name", "Please enter your name", "name_id", "Current Name");
在所有控件上,您都可以使用简单的方法设置名称、ID、类(es)、值、占位符、标签文本、必填(y/n)、禁用(y/n)、只读(y/n)、错误状态(y/n)和帮助文本
setId($id) // set the id of the control tag (<input>/<select>/etc) setClass($class), addClass($class), removeClass($class) // Edit the current class(es) setValue($value) // set the value of the control. (not filtered!!) setPlaceholder($placeholder) // set the placeholder setLabel($labeltext) // set the label text, for using when rendering in a supported form setRequired($required) // set if the control is required (note, browser can be manipulated) setDisabled($disabled) // set disabled state setReadOnly($readonle) // set readonly state setErrorState($errorstate) // set if the control is in error state setHelpText($helptext) // set helptext for under the control
创建后,您可以将其添加到面板或直接添加到表单中。
文本
您现在可以使用setType($type)
方法输入自定义类型的输入。
文本区域
在文本区域对象上,您可以使用setRows($rows)
方法设置可选的行参数。
下拉列表
要设置下拉列表控件中的数据(选项),使用以下方法
$options = array("key_value" => "Key Value"); setOptions($options);
$options需要包含一个数组,其中:[value] = "可读文本"。
面板
您可以将控件与面板结合使用,它类似于元素组,但具有特定的样式。目前有两种面板,StackPane和InlinePane
使用以下方法创建面板并向其中添加控件
$pane = new StackPane("Label Here"); $pane->addControl($control); $form->add($pane);
StackPane
StackPane将元素组合在一起,每个元素都在新的一行中。只需将其添加到下面即可。
InlinePane
InlinePane将支持的控件(复选框和单选按钮)以内联样式显示。有关更多信息,请参阅https://bootstrap.ac.cn/css/?#inline-checkboxes-and-radios。
许可协议
本项目采用MIT许可协议,请参阅LICENSE文件。
责任
我们不承担任何安全问题,使用前请务必检查代码!