blackit / semantic-form
语义UI表单助手
v1.0
2019-06-03 22:29 UTC
Requires
- php: >=7.1.3
- illuminate/support: ~5.6
- jenssegers/date: ^3.4
- nesbot/carbon: ^1.36
Requires (Dev)
- mockery/mockery: 0.9.*
- php-coveralls/php-coveralls: ^2.1
- phpunit/phpunit: ~7.0
- squizlabs/php_codesniffer: ^3.4
This package is auto-updated.
Last update: 2024-09-04 09:34:47 UTC
README
Semantic UI 表单构建器,用于 Laravel。
安装
$ composer require blackit/semantic-form
API
注意:您可以使用外观 Form::method()
或助手 form()->method()
。
打开表单
Form::open('search'); // action="search" Form::open()->get(); Form::open()->post(); Form::open()->put(); Form::open()->patch(); Form::open()->delete(); Form::open(); // default to method="GET" Form::open()->action('search'); Form::open()->url('search'); // alias for action() Form::open()->route('route.name'); Form::open()->post()->action(route('comment.store'));
打开表单(短语法,自 1.10 版起)
Form::open('search'); // action="search" method=POST Form::get('search'); // action="search" method=GET Form::post('search'); // action="search" method=POST Form::put('search'); // action="search" method=POST _method=PUT Form::patch('search'); // action="search" method=POST _method=PATCH Form::delete('search'); // action="search" method=POST _method=DELETE
CSRF 令牌
如果当前表单方法为 POST
,CSRF 令牌将自动生成。为了防止生成令牌,您可以在打开表单时调用 withoutToken()
。
Form::post('search')->withoutToken();
文本输入
Form::text($name, $value)->label('Username');
密码
Form::password($name)->label('Password');
电子邮件
Form::email($name, $value)->label('Email Address');
文本区域
Form::textarea($name, $value)->label('Note');
下拉选择框
Form::select($name, $options)->label('Choose Country'); Form::select($name, $options, $selected)->label('Choose Country'); Form::select($name, $options)->placeholder('--Select--'); Form::select($name, $options)->appendOption($key, $label); Form::select($name, $options)->prependOption($key, $label);
选择日期和时间
Form::selectDate('myDate', $startYear, $endYear)->label('Birth Date'); Form::selectDateTime('myDate', $startYear, $endYear, $intervalInMinute)->label('Schedule');
默认情况下,selectDate 和 selectDateTime 将以 _myDate
发送请求,例如 ['date'=>4, 'month'=>5, 'year'=>2016]
。要获取 2016-5-4
格式,您需要注册中间件并在路由中使用它。
protected $routeMiddleware = [ 'selectdate' => \BlackIT\SemanticForm\Middleware\SelectDateMiddleware::class, 'selectdatetime' => \BlackIT\SemanticForm\Middleware\SelectDateTimeMiddleware::class ];
Route::post('myForm', ['middleware' => ['web', 'selectdate:myDate'], function (\Illuminate\Http\Request $request) { dd($request->input('myDate')); // Will output 2016-5-4 }]);
选择范围
Form::selectRange($name, $begin, $end)->label('Number of child');
选择月份
Form::selectMonth($name, $format = '%B')->label('Month');
单选按钮
$checked = true; Form::radio($name, $value, $checked)->label('Item Label');
单选按钮组
$values = ['apple' => 'Apple', 'banana' => 'Banana']; $checkedValue = 'banana'; Form::radioGroup($name, $values, $checkedValue)->label('Select Fruit');
复选框
Form::checkbox($name, $value, $checked)->label('Remember Me');
复选框组
$values = ['apple' => 'Apple', 'banana' => 'Banana']; $checkedValue = 'banana'; Form::checkboxGroup($name, $values, $checkedValue)->label('Select Fruit');
文件
Form::file($name);
输入包装器
Form::input($name, $defaultvalue); Form::input($name, $defaultvalue)->appendIcon('search'); Form::input($name, $defaultvalue)->prependIcon('users'); Form::input($name, $defaultvalue)->appendLabel($label); Form::input($name, $defaultvalue)->prependLabel($label); Form::input($name, $defaultvalue)->type("password");
参考: https://semantic-ui.com.cn/elements/input.html
图片(尚未实现)
Form::image($name);
日期选择器(实验性)
// somewhere in view Form::datepicker($name, $value, $format); // don't forget to put this somewhere on your view @include('semantic-form::scripts.calendar') // Valid $format are: // DD -> two digit date // MM -> two digit month number // MMMM -> month name (localized) // YY -> two digit year // YYYY -> full year // To convert localized format to standard (SQL) datetime format, you can use Jenssegers\Date\Date library (already included): // Jenssegers\Date\Date::createFromFormat('d F Y', '12 februari 2000')->startOfDay()->toDateTimeString(); // Jenssegers\Date\Date::createFromFormat('d F Y', '12 februari 2000')->startOfDay()->toDateString();
有关进一步讨论,请参阅 Semantic-Org/Semantic-UI#3256。请记住,您必须在自己的网站上包含 calendar.js 和 calendar.css。
隐藏
Form::hidden($name, $value);
按钮
Form::button($value);
提交
Form::submit($value);
模型绑定
版本 1
{!! Form::bind($model) !!}
版本 2
// as parameter for method open() {!! Form::open($route, $model) !!} // or chaining it {!! Form::bind($model)->get($route) !!}
警告
// This is OK in version 1, but will produce error in version 2 {!! Form::bind($model) !!}
宏
宏定义,将其放在应用程序的任何地方,例如 AppServiceProvider
Form::macro('trix', function ($id, $name, $value = null) { return sprintf( "%s %s", Form::hidden($name, $defaultValue)->id($id), "<trix-editor input='{$id}'></trix-editor>" ); });
然后像调用任何其他方法一样调用它
Form::trix('contentId', 'content', '<b>some content</b>');
操作
// Method 1 Form::action(Form::submit('Save'), Form::button('cancel')); // Method 2 // Assumed you already define some macros: Form::macro('submit', function(){ return form()->submit('Submit'); }); Form::macro('cancel', function(){ return form()->button('Cancel'); }); // Then you can just call macro name as string Form::action('submit', 'cancel'); // Method 3 // Even further, you can define macro thats just wrap several buttons: SemanticForm::macro('default', function(){ return new \BlackIT\SemanticForm\Elements\Wrapper(form()->submit('Submit'), form()->submit('Submit')); }); // And then make the call simplier: Form::action('default');
通用功能
对于每个表单元素,您可以调用以下方法并链式调用
- id($string)
- addClass($string)
- removeClass($string)
- attribute($name, $value)
- data($name, $value)
- hint($text)(自 1.10.0 版起)
- hint($text, $class = "hint")(自 1.10.2 版起)
全局覆盖提示类(自 1.10.2 版起)
// Put this on every request, e.g. in AppServiceProvider BlackIT\SemanticForm\Elements\Hint::$defaultClass = 'custom-class';
示例
Form::text($name, $value)->label('Username')->id('username')->addClass('foo'); Form::text($name, $value)->label('Username')->data('url', 'http://id-laravel.com'); Form::password($name, $value)->label('Password')->hint('Minimum 6 characters'); Form::password($name, $value)->label('Password')->hint('Minimum 6 characters', 'my-custom-css-class');
中间件
- \BlackIT\SemanticForm\Middleware\SelectDateMiddleware
- \BlackIT\SemanticForm\Middleware\SelectDateTimeMiddleware
致谢
SemanticForm 受 AdamWathan\Form 的启发。