咨询 / form
HTML表单构建器
Requires
- league/plates: ^3.1
- tightenco/collect: ^5.3
Requires (Dev)
- konsulting/laravel-transformer: ^0.5.2
- laravel/framework: ^5.3
- phpunit/phpunit: ^5.6
Suggests
- konsulting/laravel-transformer: Tranform Request data simply, recommended for use with DateTimeRulePack
README
A library to simplify form building in php applications. It uses a template approach, supplied by Plates. It is inspired by the Laravel Collective Form library and a previous generations of form builder developed by Konsulting.
You can use the library in any PHP project, but we have included some helpers for usage within a Laravel Project.
Documentation is still being developed, so please bear with us.
安装
We recommend using composer to install the library.
composer require konsulting/form-builder
在 Laravel 中使用
The package will auto-register the Service Provider and Facade in Laravel 5.5+. In earlier versions of Laravel, you will need to manually register them yourself in your config/app.php
.
"providers" => [ ... "Konsulting\\FormBuilder\\Laravel\\FormBuilderServiceProvider" ], "aliases" => [ ... "Form" => "Konsulting\\FormBuilder\\Laravel\\FormBuilderFacade" ]
You will then be able to access the FormBuilder instance though the Facade in your views (or anywhere else you need to).
// For example Form::text('name', $yourName)->withLabel('Your Name')
在 Laravel 之外使用
To begin using the form builder, you need to construct it using the Plates Engine (which essentially tells it which templates to use) and a class resolver (which effectively tells the build where to look for each form elements' class).
use League\Plates\Engine; use Konsulting\FormBuilder\FormBuilder; use Konsulting\FormBuilder\ClassResolver; //... $builder = new FormBuilder( new Engine(__DIR__ . '/../../vendor/konsulting/form-builder/partials/bootstrap3'), new ClassResolver('Konsulting\\FormBuilder\\Elements\\') );
There is a simple API to build up your form elements. Each element shares a set of common methods, and we have set up the base set of html form elements.
共享方法
设置属性
withLabel(label)
和withoutLabel
withError(message)
和withoutError
withFeedback(type, message)
和withoutFeedback
withHelp(message)
withAddon(content, position=before|after)
- specific addon to the input (e.g. calendar icon to the right of an input box)prepend(content)
- place content before the whole html blockappend(content)
- place content after the whole html blockwithAttributes(['name' => value, ...])
- set a range of attributes, or those without specific setters
检索属性
attributes([array_of_keys_wanted])
attributesExcept([array_of_keys_to_exclude])
表单元素
input(type, name, value)
checkbox(name, value)
radio(name, value)
select(name, options, selected)
textarea(name, value)
date(name, value)
time(name, value)
datetime(name, value)
日期和时间
Dates and time have an additional method, split()
, which is used to generate input boxes that are split into each component of the date/time. There is a DateTimeFormats helper class, which is used to set the formats for user input/display and for persistence.
The helper also contains functions used to help split the date into components or re-combine it based on the settings.
When using the split()
method, your dates and times will need to be recombined when validating them, for example.
贡献
Contributions are welcome and will be fully credited. We will accept contributions by Pull Request.
请
- 使用 PSR-2 编码标准
- 添加测试,如果您不确定如何做,请提问。
- 记录行为变化,包括 readme.md。
测试
We use PHPUnit for this package.
运行测试使用 PHPUnit: vendor/bin/phpunit