此包已被弃用且不再维护。未建议替代包。

轻松创建渲染和验证的表单。

0.4.0 2014-12-15 13:32 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:33:39 UTC


README

Build Status Packagist Software License

Reform使在PHP中创建表单变得简单。创建一个表单,添加行和验证,然后调用render()。其余的一切都将自动完成 - 检查提交,验证数据,设置值,创建标签和错误消息,处理CSRF...

为了更好地控制,表单可以逐行渲染,甚至可以单独渲染。您可以使用一些功能,而不会受到其他功能的干扰。

功能

  • 许多行类型和验证规则。添加自定义类型以匹配您的要求是微不足道的。
  • 不同的渲染器用于向表单应用样式(例如Bootstrap)。更改渲染器只需一行代码。
  • Symfony HttpFoundation集成以自动提交表单。
  • 安全措施,如蜜罐字段、计时器和CSRF保护。通过添加Blockade安全库来自动处理这些异常。
  • 事件以自定义表单的行为。

快速入门

一个简单的表单,包含用户名和密码字段。

$form = new Reform\Form\Form('/login');
$form->text('username');
$form->password('password');
$form->submit('login');

echo $form->render();

现在添加了一些验证。

$form = new Reform\Form\Form('/login');
$form->text('username')
    ->addRule(new Rule\Required('Did you forget your name?'))
    ->addRule(new Rule\Regex('`[A-z.]+`'))
$form->password('password')
    ->addRule(new Rule\Required());
$form->submit('login');

使用Symfony请求对象自动提交表单。如果使用了正确的HTTP方法并且所有字段都通过了必要的验证,则表单被认为是有效的。

无论有效与否,提交后字段都会填充提交的数据。

$request = Request::createFromGlobals();
$form->handle($request);

if ($form->isValid()) {
    //the form was submitted correctly - the correct http method was
    //used and all validation rules passed

    //perform the login and redirect
    login_user();
    redirect_to_home_page();
}
//the form was either not submitted or failed the validation. $form
//now has any submitted parameters bound to it, so all we need to do
//is render the form again. Any values and errors will be added
//automatically.

echo $form->render();

查看docs/以获取更多文档。

安装

使用composer安装

{
    "require": {
        "glynnforrest/reform": "0.4.*"
    }
}

查看示例

composer install
cd examples/
bower install
php -S localhost:8000

然后在您的网页浏览器中访问localhost:8000

这些说明假定您已安装composer、bower和PHP 5.4。

许可

MIT,有关详细信息,请参阅LICENSE。

版权所有 2014 Glynn Forrest