helmut/forms

一个可定制和可测试的表单抽象库。

v1.1.8 2017-04-06 05:40 UTC

This package is not auto-updated.

Last update: 2024-09-28 20:14:49 UTC


README

Build Status Scrutinizer Code Quality Code Coverage Software License

一个可定制和可测试的表单抽象库。将其视为一个强化版的请求模型。我们以不同的方式处理表单。表单简化了复杂性,让您专注于设计。使用内置的默认字段,或构建自己的可重用和可测试字段库,并将其应用于您构建的每个应用程序。

  • 使表单可测试
  • 抽象请求而非HTML
  • 提供重用字段组件的模型
  • 以通常的方式设计字段
  • 使用mustache、twig或blade进行渲染
  • 内置验证

文档

安装

要获取Forms的最新版本,只需使用Composer引入项目。

$ composer require helmut/forms

当然,您也可以手动更新require块并运行composer update

{
    "require": {
        "helmut/forms": "~1.0"
    }
}

如果您使用Laravel,则需要注册服务提供者。打开config/app.php并在providers数组中添加Helmut\Forms\Providers\Laravel::class键。

用法

步骤 1

创建一个继承自\Helmut\Forms\Form的类。

// File: app/Forms/Form.php

namespace App\Forms;

class Form extends \Helmut\Forms\Form {

}

步骤 2

现在您可以创建一个表单。

$form = new \App\Forms\Form;

或者在Laravel中,直接在路由或控制器方法中声明类型提示。

// File: app/Http/routes.php

Route::any('/', function(\App\Forms\Form $form) {

	// Now you can access $form

});

步骤 2

定义字段以构建表单。

$form->email('email')->label('Email Address')->required();
$form->password('password')->label('Password')->required();
$form->checkbox('remember')->label('Remember me');
$form->button('login')->label('Sign In');

或者,您还可以创建一个仅为此特定表单的类,它继承自\App\Forms\Form。然后可以在define方法中定义字段,它们将自动添加。

// File: app/Forms/Login.php

namespace App\Forms;

class Login extends Form {

	public function define()
	{
		$this->email('email')->label('Email Address')->required();
		$this->password('password')->label('Password')->required();
		$this->checkbox('remember')->label('Remember me');
		$this->button('login')->label('Sign In');
	} 

}

步骤 4

现在您可以渲染表单并处理提交。

$form = new \App\Forms\Login;

if ($form->completed()) {

	// The form has been submitted and passed validation
}
	
echo $form->render();

或者在Laravel中

// File: app/Http/Controllers/LoginController.php

class LoginController extends Controller {
	
    public function handleLoginForm(\App\Forms\Login $form)
    {
    	if ($form->completed()) {

			// The form has been submitted and passed validation

        }

        return view('login.form', compact('form'));
    }

}
// File: resources/views/login/form.blade.php

@extends('template')

@section('content')

	<h3>Sign In</h3>

    {!! $form !!}

@endsection

步骤 6

查看表单!

login

API参考

这些方法允许您与表单交互

	// Fields

	$form->button('register')					// Create a button
	$form->text('foo')							// Create a text field
	$form->name('foo')							// Create a name field
	$form->email('foo')							// Create an email field
	$form->number('foo')						// Create a numeric field
	$form->password('foo')						// Create a password field
	$form->paragraph_text('foo')				// Create paragraph text field
	$form->checkbox('foo')						// Create a checkbox field
	$form->checkboxes('foo')					// Create checkboxes field
	$form->dropdown('foo')						// Create dropdown field
	$form->search('foo')						// Create a search box field

	// Applying Modifiers

	$form->text('foo')->required()				// Make a field required
	$form->text('foo')->default('bar')			// Set a default value
	$form->checkbox('foo')->checked()			// Make it checked
	$form->checkbox('foo')->unchecked()			// Make it unchecked
	$form->dropdown('foo')->options([...])		// Add dropdown options

	// Pre-Filling

	$form->defaults($array)						// Load defaults from an array
	$form->defaults($user, $company, ...)		// Load defaults from model/s

	// Rendering

	$form->render() 							// Generate form
	$form->render('flat') 						// Generate form using flat templates

	// Processing

	$form->valid() 								// Validate the form
	$form->valid('name') 						// Validate a specific field
	$form->invalid() 							
	$form->invalid('name') 						
	$form->submitted() 							// Check if the form has been submitted
	$form->submitted('register') 				// Check if submitted using a specific button
	$form->completed() 							// Check if submitted and valid

	// Retrieving Values

	$form->all() 								// Get all the values
	$form->get('foo') 							// Get the foo field values
	$form->get('foo', 'bar') 					// Get the foo[bar] field value

	// Filling Models

	$form->fill($user) 							// Fills all fields in user model
	$form->fill($user, 'name')	 				// Fills just the name fields
	$form->fill($user, ['name', 'email'])	 	// Fills just the name and email fields

字段类型

这些字段类型已默认包含

按钮

$form->button('foo')->label('Foo')
$form->submitted('foo') // Returns true if form was submitted using this button
$form->completed('foo') // Returns true if form both submitted and valid

示例

$form->button('signup')->label('注册');

button

文本

$form->text('foo')->label('Foo')->default('bar')->required()
$form->get('foo') // Returns 'bar'

验证:between(min, max)min(num)max(num)alphaalpha_numalpha_dashin(array)not_in(array)

示例

$form->text('address')->label('地址')->required();

text

名称

$form->name('foo')->label('Foo')->default(['first' => 'Bar', 'surname' => 'Baz'])->required()
$form->get('foo') // Returns ['foo_first' => 'Bar', 'foo_surname' => 'Baz', 'foo' => 'Bar Baz']
$form->get('foo', 'surname') // Returns 'Baz'

示例

$form->name('name')->label('姓名')->required();

name

电子邮件

$form->email('foo') // Same as `text` but with email validation added.

示例

$form->email('email')->label('电子邮件地址')->required();

email

数字

$form->number('foo')->label('Foo')->default('123')->required()
$form->get('foo') // Returns '123'

验证:between(min, max)min(num)max(num)integerin(array)not_in(array)

示例

$form->number('age')->label('年龄')->integer()->min(18)->required();

number

密码

$form->password('foo')->label('Foo')->required()
$form->get('foo') // Returns 'hashed_bar'
$form->password('foo')->matches('other_hash') // Returns true/false

示例

$form->password('password')->label('密码')->required();

password

段落文本

$form->paragraph_text('foo')->label('Foo')->default('bar')->required()
$form->get('foo') // Returns 'bar'

示例

$form->paragraph_text('comments')->label('评论');

paragraph_text

复选框

$form->checkbox('foo')->label('Foo')->required()
$form->checkbox('foo')->checked() // Check the box
$form->checkbox('foo')->unchecked() // Uncheck the box
$form->get('foo') // Returns true/false

示例

$form->checkbox('subscribe')->label('订阅我们的通讯');

checkbox

复选框组

$form->checkboxes('foo')->label('Foo')->options(['bar' => 'Bar', 'baz' => 'Baz'])->required()
$form->checkboxes('foo')->checked() // Check all the boxes
$form->checkboxes('foo')->checked(['bar']) // Check some of the boxes
$form->checkboxes('foo')->unchecked() // Uncheck all the boxes
$form->checkboxes('foo')->unchecked(['baz']) // Uncheck some of the boxes
$form->get('foo') // Returns ['foo_bar' => false, 'foo_baz' => false]

示例

$form->checkboxes('interests')->label('兴趣')->options(['golf' => '高尔夫', 'swimming' => '游泳', 'dancing' => '舞蹈', 'reading' => '阅读'])->required();

checkboxes

下拉菜单

$form->dropdown('foo')->label('Foo')->options(['bar' => 'Bar', 'baz' => 'Baz'])->default('baz')->required()
$form->get('foo') // Returns 'baz'

示例

$form->dropdown('colour')->label('颜色')->options(['red' => '红色', 'green' => '绿色', 'blue' => '蓝色']);

dropdown

搜索

$form->search('foo')
$form->get('foo') // Returns ['foo' => 'bar', 'foo_button' => true]	

示例

$form->search('search');

search

定制

Forms(表单)被设计为一个框架,开发人员可以在其基础上构建模块库,以简化处理复杂请求的重复性工作。包含了一些基本的字段和模板,但预期您会以此为基础进行定制。通过自定义,您可以设计、构建和测试它们一次,然后将它们应用到任何应用程序中。

模板

默认提供了一些基本的模板包,它们与常见的CSS框架(如BootstrapFoundation)兼容。这些应该为定制提供了一个很好的基础。Forms与CSS预处理器兼容,且正在计划支持JeetSingularityNeat

$form->setTemplate('bootstrap') 		// Default
$form->setTemplate('foundation')

$form->setTemplate('jeet')              // Coming soon...
$form->setTemplate('singularity')       // Coming soon...
$form->setTemplate('neat')              // Coming soon...

语言

$form->setLanguage('en') 	// Set language to english
$form->setLanguage('es') 	// Idioma establecida en español

插件

$form->addPlugin('feedback');   // Instant validation feedback
$form->addPlugin('memory');   // Autosave as you type

安全

如果您发现任何与安全相关的问题,请通过电子邮件联系helmut.github [at] gmail.com,而不是使用问题跟踪器。所有安全漏洞都将得到及时处理。

许可协议

MIT许可(MIT)。有关更多信息,请参阅许可文件