sukohi/form-strap

一个主要为Laravel开发,用于生成Bootstrap表单输入标签的PHP包,可以显示错误信息。

2.0.1 2015-08-14 02:33 UTC

This package is not auto-updated.

Last update: 2024-09-14 16:44:59 UTC


README

一个主要为Laravel开发,用于生成Bootstrap表单输入标签的PHP包,可以自动显示错误、标签和警告。
(适用于Laravel 5+。 适用于Laravel 4.2)

安装

在composer.json中添加此包名

"require": {
  "sukohi/form-strap": "2.*"
}

执行composer命令

composer update

在app.php中注册服务提供者

'providers' => [
    ...Others...,  
    Sukohi\FormStrap\FormStrapServiceProvider::class,
]

也别名

'aliases' => [
    ...Others...,  
    'FormStrap'   => Sukohi\FormStrap\Facades\FormStrap::class
]

要求

使用(使用blade)

文本

{!! \FormStrap::text('name_1', 'text') !!}

标签

{!! \FormStrap::text('name_2', 'text')->label('LABEL_1') !!}
			
{!! \FormStrap::text('name_3', 'text')->label('LABEL_2', ['class' => 'text-danger']) !!}
			
{!! \FormStrap::text('name_4', 'text')->label('LABEL_3')->icon('<i class="fa fa-home"></i>', 'left') !!}

密码

{!! \FormStrap::password('name_9', $options = []) !!}

文本区域

{!! \FormStrap::textarea('name_10', 'Text', $options = []) !!}

单选按钮

{!! \FormStrap::radio('name_11', ['value' => 'label'], 'checked_value', $options = []) !!}
		
{!! \FormStrap::radio('name_12', [
		'value_1' => 'label_1', 
		'value_2' => 'label_2', 
		'value_3' => 'label_3'
	], 'checked_value', $options = [], $separator = '&nbsp;') !!}

复选框

{!! \FormStrap::checkbox('name_13', ['value' => 'label'], ['checked_value'], $options = []) !!}
			
{!! \FormStrap::checkbox('name_14', [
	'value_1' => 'label_1', 
	'value_2' => 'label_2', 
	'value_3' => 'label_3'
], [
	'checked_value_1', 
	'checked_value_2'
], $options = [], $separator = '&nbsp;') !!}

选择框

{!! \FormStrap::select('name_15', [
	'' => '', 
	'value_1' => 'label_1', 
	'value_2' => 'label_2', 
	'value_3' => 'label_3'
], 'selected_value', $options = []) !!}

{!! \FormStrap::select('name_15_2', [
    '' => '',
    'value_1' => 'label_1',
    'value_2' => 'label_2',
    'value_3' => 'label_3'
], 'selected_value', $options = [], $redirect_url = 'http://example.com/{selected_value}') !!}
  • 当设置$redirect_url时,选择框将具有"onchange"事件以重定向URL。
  • {selected_value}将被替换为您选择的值。

文件

{!! \FormStrap::file('name_16', $options = []) !!}

{!! \FormStrap::file('name_17')->multiple() !!}

{!! \FormStrap::file('name_18')->accept('image/*') !!}

隐藏字段

{!! \FormStrap::hidden([
	'name_19' => 'value_1', 
	'name_20' => 'value_2', 
	'name_21' => 'value_3'
]) !!}

自定义视图

{!! \FormStrap::view('name_22', 'custom.view', $parameters = []) !!}

提交

{!! \FormStrap::submit('Submit <i class="fa fa-home"></i>', ['class' => 'btn btn-success btn-sm']) !!}

{!! \FormStrap::submit('Submit', ['class' => 'btn btn-success btn-sm'])
		->cancel('url', 'CANCEL', ['class' => 'btn btn-default btn-sm']) !!}

{!! \FormStrap::submit('Submit')->cancel('url')->right() !!}

验证属性名称(隐藏标签)

{!! \FormStrap::attributeNames($key = 'attribute_names') !!}

 or you can add/overwrite attributes using array.

{!! \FormStrap::attributeNames('attribute_names', [
    'attribute_1' => 'value_1', 
    'attribute_2' => 'value_2',
    'attribute_3' => 'value_3'
]) !!}

然后在控制器中

$validator->setAttributeNames(Request::input('attribute_names'))

选项标签

这种方法是用来获取单选按钮、复选框和选择框的选中标签。
首先,将以下内容设置为在视图中生成隐藏标签。

{!! \FormStrap::optionLabels() !!}

// or you can use custom name with additional values

{!! \FormStrap::optionLabels('custom_hidden_name', [
    'key' => ['1' => 'value_1', '2' => 'value_2']
]) !!}

然后

$option_labels = \FormStrap::selectedOptionLabels('key');

警告

当以这种方式重定向时。

return back()->with('danger', 'Error occured!');

然后

{!! \FormStrap::alert('danger') !!}
{!! \FormStrap::alert('warning') !!}
{!! \FormStrap::alert('info', $dismissable = true) !!}
{!! \FormStrap::alert('success', false) !!}

// If you'd like to add icon(s)

{!! \FormStrap::alert()->icons([

	'danger' => '<i class="fa fa-***"></i> ', 
	'warning' => '<i class="fa fa-***"></i> ', 
	'primary' => '<i class="fa fa-***"></i> ', 
	'info' => '<i class="fa fa-***"></i> '

]) !!}

(在这种情况下,只显示第一个。)
您还可以使用数组和使用空值来覆盖所有级别。

{!! \FormStrap::alert(['danger', 'warning']) !!}
{!! \FormStrap::alert() !!}

检查是否存在任何警告

@if(\FormStrap::hasAlert())
    Alert exists!
@endif

or 

@if(\FormStrap::hasAlert(['danger', 'info']))
    Alert exists!
@endif

CSS

\FormStrap::css('input', 'your-class-name');  // for input-tag
\FormStrap::css('label', 'your-class-name');  // for label-tag
\FormStrap::css('group', 'your-class-name');  // for div-tag surrounding input and label 
\FormStrap::css('content', 'your-class-name');  // for div-tag surrounding input

许可协议

本软件包根据MIT许可协议授权。

版权所有2014 Sukohi Kuhoh