typicms / form
一个基本的、与框架无关的表单构建包,具有记住旧输入和检索错误消息等额外功能。
Requires
- php: ^8.0.2
Requires (Dev)
- illuminate/support: >=9.0
- mockery/mockery: ^1.4.4
- nunomaduro/larastan: ^2.0
- orchestra/testbench: ^7.4
- php-coveralls/php-coveralls: ~2.5.2
- phpunit/php-code-coverage: ^9.2
- phpunit/phpunit: ^9.5.10
README
使用直观的语法构建HTML表单。
此包最初由Adam Wathan创建。
安装
您可以通过在项目根目录的终端中运行此命令通过Composer安装此包
composer require typicms/form
Laravel
此包非常适合作为Laravel 5中删除的表单构建器的替代品。API不同,但所有功能都存在。
如果您使用的是Laravel 4或5,您可以注册FormServiceProvider以自动获得旧输入和错误消息功能。
为此,只需更新您的config/app.php
中的providers
数组
'providers' => [ //... 'TypiCMS\Form\FormServiceProvider' ],
您还可以选择使用外观,通过在config/app.php
中添加别名来实现
'aliases' => [ //... 'Form' => 'TypiCMS\Form\Facades\Form', ],
请注意,在Laravel 4中,已经有一个内置的表单外观为Form Builder。如果您想使用两个,请使用不同的别名。如果您只想使用这个,请删除指向Illuminate组件的Form别名。
基本用法
入门
首先,实例化一个表单构建器...
$builder = new TypiCMS\Form\FormBuilder;
接下来,使用表单构建器构建一个元素。例如
// <input type="text" name="email" value="example@example.com" required="required"> <?= $builder->text('email')->value('example@example.com')->required(); ?>
- 所有元素都支持方法链,因此您可以向元素添加所需的所有选项。
- 所有元素都实现了
__toString()
,因此无需手动渲染。
打开表单
// <form method="POST"> <?= $builder->open(); ?> // <form method="GET"> <?= $builder->open()->get(); ?> // <form method="POST"> // <input type="hidden" name="_method" value="PUT"> <?= $builder->open()->put(); ?> // <form method="POST"> // <input type="hidden" name="_method" value="DELETE"> <?= $builder->open()->delete(); ?> // <form method="POST" action="/test"> <?= $builder->open()->action('/test'); ?> // <form method="POST" action="" enctype="multipart/form-data"> <?= $builder->open()->multipart() ?> // <form method="POST" action="" enctype="custom"> <?= $builder->open()->encodingType("custom") ?>
文本和密码字段
文本和密码字段具有相同的接口。
// <input type="text" name="email"> <?= $builder->text('email'); ?> // <input type="text" name="email" id="email_field"> <?= $builder->text('email')->id('email_field'); ?> // <input type="password" name="password" class="required"> <?= $builder->password('password')->addClass('required'); ?> // <input type="text" name="email" value="example@example.com" required="required"> <?= $builder->text('email')->value('example@example.com')->required(); ?>
其他可用方法
placeholder($string)
optional()
defaultValue($string)
disable()
enable()
文本区域
文本区域与常规文本字段具有相同的接口,但有几个额外有用的方法。
// <textarea name="bio" rows="5" cols="50"></textarea> <?= $builder->textarea('bio')->rows(5); ?> // <textarea name="bio" rows="10" cols="20"></textarea> <?= $builder->textarea('bio')->cols(20); ?> // <textarea name="bio" rows="5" cols="20" class="important">My biography</textarea> <?= $builder->textarea('bio')->rows(5)->cols(20)->addClass('important')->value('My biography'); ?>
复选框和单选按钮
// <input type="checkbox" name="terms" value="1"> <?= $builder->checkbox('terms'); ?> // <input type="checkbox" name="terms" value="1" checked="checked"> <?= $builder->checkbox('terms')->check(); ?> // <input type="checkbox" name="terms" value="1"> <?= $builder->checkbox('terms')->uncheck(); ?> // <input type="checkbox" name="terms" value="1" checked="checked"> <?= $builder->checkbox('terms')->defaultToChecked(); ?> // <input type="checkbox" name="terms" value="agree"> <?= $builder->checkbox('terms')->value('agree'); ?> // <input type="radio" name="color" value="red"> <?= $builder->radio('color', 'red'); ?>
下拉菜单
// <select name="birth_year"></select> <?= $builder->select('birth_year'); ?> // <select name="birth_year"> // <option value="0">1990</option> // <option value="1">1991</option> // <option value="2">1992</option> // </select> <?= $builder->select('birth_year', [1990, 1991, 1992]); ?> // <select name="birth_year"> // <option value="1990">1990</option> // <option value="1991">1991</option> // <option value="1992">1992</option> // </select> <?= $builder->select('birth_year', ['1990' => 1990, '1991' => 1991, '1992' => 1992]); ?> // <select name="birth_year"> // <optgroup label="Ontario"> // <option value="toronto">Toronto</option> // <option value="ottawa">Ottawa</option> // </optgroup> // <optgroup label="Quebec"> // <option value="montreal">Montreal</option> // <option value="quebec_city">Quebec City</option> // </optgroup> // </select> $options = [ 'Ontario' => [ 'toronto' => 'Toronto', 'ottawa' => 'Ottawa', ], 'Quebec' => [ 'montreal' => 'Montreal', 'quebec_city' => 'Quebec City', ] ]; <?= $builder->select('birth_year', $options); ?> // <select name="birth_year"> // <option value="1">1990</option> // </select> <?= $builder->select('birth_year')->addOption('1', 1990); ?> // <select name="birth_year"> // <option value="1">1990</option> // <option value="2">1991</option> // <option value="3" selected>1992</option> // </select> <?= $builder->select('birth_year', ['1' => 1990, '2' => 1991, '3' => 1992])->select('3'); ?>
按钮
// <button type="button">Click Me</button> <?= $builder->button('Click Me'); ?> // <button type="submit">Sign Up</button> <?= $builder->submit('Sign Up'); ?> // <button type="reset">Reset Form</button> <?= $builder->reset('Reset Form'); ?> // <button type="submit" class="js-submit">Sign Up</button> <?= $builder->submit('Sign Up')->addClass('js-submit'); ?>
隐藏输入
// <input type="hidden" name="secret" value="my-secret-value"> <?= $builder->hidden('secret')->value('my-secret-value'); ?>
标签
基本标签
// <label>Email</label> <?= $builder->label('Email'); ?> // <label for="email">Email</label> <?= $builder->label('Email')->forId('email'); ?>
包装另一个元素
// <label>Email<input type="text" name="email"></label> <?= $builder->label('Email')->before($emailElement); ?> // <label><input type="text" name="email">Email</label> <?= $builder->label('Email')->after($emailElement); ?>
设置属性
// Attributes can be set with attribute(...) // <input type="text" name="foobar" min="4"> <?= $builder->text('foobar')->attribute('min', 4); ?> // Or by calling the attribute name as a method // <input type="text" name="foobar" min="4"> <?= $builder->text('foobar')->min(4); ?> // Setting data-* attributes // <input type="text" data-foo="bar" name="foobar"> <?= $builder->text('foobar')->data('foo', 'bar'); ?> // Multiple data-* attributes can be set at once // <input type="text" data-foo="bar" data-bar="foo" name="foobar"> <?= $builder->text('foobar')->data(['foo' => 'bar', 'bar' => 'foo']); ?>
记住旧输入
如果您因为验证错误而重定向回表单,表单构建器可以记住旧输入并预填充表单字段。
为此,您必须创建一个实现OldInputInterface
的类,并将其传递给表单构建器
$builder->setOldInputProvider($myOldInputProvider);
现在,如果可用,表单元素将自动填充用户的旧输入数据。
这很好地与defaultValue()
方法一起工作,允许您设置默认值,如果用户已经提交了表单,则旧输入将覆盖该值。
此包附带一个Laravel实现,称为
IlluminateOldInput
。
错误消息
表单构建器还允许您轻松检索表单元素的错误消息。为此,只需实现ErrorStoreInterface
并将其传递给表单构建器
$builder->setErrorStore($myErrorStore);
此包附带一个Laravel实现,称为
IlluminateErrorStore
。
// Check if the form has an error for an element $builder->hasError('email'); // Retrieve the error message for an element $builder->getError('email');
您还可以向getError()
提供format
参数,以清理您的标记。您不需要这样做
<?php if ($builder->hasError('email')): ?> <span class="error"><?= $builder->getError('email'); ?></span> <?php endif; ?>
...只需这样做,如果存在格式化的消息,将显示该消息,否则不显示。
<?= $builder->getError('email', '<span class="error">:message</span'); ?>
CSRF保护
假设您在实例化表单构建器时设置了CSRF令牌(或者您正在使用Laravel),您可以像这样轻松地将CSRF令牌添加到您的表单中
<?= $builder->token(); ?>
数据绑定
有时您可能有一个表单,其中所有字段都匹配系统中的某种对象或数组上的属性,并且您希望用户能够编辑这些数据。数据绑定通过允许您将对象或数组绑定到表单中,从而自动为所有字段提供默认值,这使得数据绑定变得非常简单。
$model->first_name = "John"; $model->last_name = "Doe"; $model->email = "john@example.com"; $model->date_of_birth = new DateTime('1985-05-06'); <?= $builder->open(); ?> <?= $builder->bind($model); ?> <?= $builder->text('first_name'); ?> <?= $builder->text('last_name'); ?> <?= $builder->email('email'); ?> <?= $builder->date('date_of_birth'); ?> <?= $builder->close(); ?>
这将与Laravel的Eloquent模型无缝配合工作。
在使用数据绑定时,旧输入将优先于任何绑定的值,因此您仍然可以轻松地将用户重定向回表单,并在不丢失任何已输入数据的情况下处理任何验证错误。
注意:在创建任何其他表单元素之前,请务必进行
bind
。