azuracast/azuraforms

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

PHP的一个现代化、命名空间化、由配置驱动的表单引擎。

1.2 2019-08-20 04:30 UTC

This package is auto-updated.

Last update: 2021-12-05 01:28:47 UTC


README

AzuraForms 是一个轻量级、自包含的表单引擎,类似于 Zend Forms 或 Symfony Forms(但不依赖于框架),允许您将表单构建为 PHP 数组,然后自动处理客户端表单的渲染以及服务器端表单输入的处理和验证。

AzuraForms 的某些功能包括

  • 每次渲染表单时自动生成 CSRF 会话令牌,并在提交时检查。
  • 在表单中显示用户输入时内置转义
  • 支持字段集
  • 能够为每个字段指定一个子数组,其中将填充数据并存储输入
  • 支持文件上传和验证上传的文件类型
  • 根据是否存在文件元素自动检测表单所需的 "enctype" 参数

默认情况下,AzuraForms 的 HTML 输出使用标准的 Bootstrap 3 表单模板样式。

该项目最初的目的为 AzuraCast 应用程序提供一个轻量级表单引擎。您可以在该应用程序中找到使用 AzuraForms 渲染的复杂表单的出色示例。

安装

AzuraForms 是一个 Composer 包,您可以通过运行以下命令将其包含到项目中:

composer require azuracast/azuraforms

快速入门

AzuraForms 使用的配置数组如下所示:

<?php
return [
    'method' => 'post',
    'elements' => [

        'username' => [
            'email',
            [
                'label' => 'E-mail Address',
                'class' => 'half-width',
                'spellcheck' => 'false',
                'required' => true,
            ]
        ],

        'password' => [
            'password',
            [
                'label' => 'Password',
                'class' => 'half-width',
                'required' => true,
            ]
        ],

        'submit' => [
            'submit',
            [
                'type' => 'submit',
                'label' => 'Log in',
                'class' => 'btn btn-lg btn-primary',
            ]
        ],
    ],
];

AzuraForms 旨在原生支持 PSR-7 ServerRequestInterface

如果您不想使用支持 PSR-7 的框架,可以使用许多 PSR-17 工厂库之一进行转换,许多库实现了 fromGlobals 方法,该方法可以从全局变量创建 ServerRequest

您的控制器代码可能如下所示:

<?php
/** @var Psr\Http\Message\ServerRequestInterface $request */

$form_config = include('form_config.php');
$defaults = [
    'email' => 'foo@bar.com',
];

$form = new \AzuraForms\Form($form_config, $defaults);

if ($form->isValid($request)) {
    $data = $form->getValues();

    // Process your submission here...
}

echo $form->render();
?>

配置格式

AzuraForms 使用的配置格式旨在具有灵活性,对于简单的表单来说简单易用,而对于更强大的表单来说也足够灵活。它类似于多年前 Zend Forms 1.0 使用的平面文件配置样式。

<?php
return [
    // Use 'groups' to denote fieldsets
    'groups' => [

        'fieldset_name' => [
            'legend' => 'My Fieldset',
            'description' => 'The description of the fieldset.',
            'elements' => [

                // The key is the name of the element
                'field_foo' => [
                    'text', // The first item is the field type
                    [
                        // The second item contains configuration options and attributes
                        'label' => 'Foo',
                        'required' => true,

                        // Any attributes that aren't configuration options will automatically
                        // be applied to the HTML element when rendered.
                        'class' => 'text-danger',
                        'autocomplete' => 'off',
                    ],
                ],

            ],
        ],

    ],

    // You can also list elements directly, without any fieldset
    'elements' => [

        'field_bar' => [
            'field_type',
            [
                'label' => 'Bar',
                'required' => true,
            ],
        ],

    ]
];

字段参考

文本样式输入字段

  • text: <input type="text">
  • email: <input type="email">,具有内置电子邮件地址验证
  • url: <input type="url">,具有内置 URL 验证
  • date: <input type="date">
  • 时间: <input type="time">
  • 数字: <input type="number">
  • 密码: <input type="password">
  • 多行文本框: <textarea>

单选字段

  • 单选按钮: 带有单独标签的单选按钮
  • 下拉选择: 接受单个响应的 <select> 下拉字段

多选字段

  • 复选框: 一个或多个带有单独标签的复选框,作为数组返回
  • 多选下拉: 接受多个响应并作为数组返回的 <select> 字段

按钮字段

  • 按钮: <input type="button">
  • 提交: <input type="submit">

其他字段

  • 标记: 支持将原始HTML嵌入到表单的中间
  • reCAPTCHA: 支持ReCAPTCHA集成
  • 文件: <input type="file">
  • csrf: CSRF保护令牌,默认自动添加到表单中。

许可证

AzuraForms遵循MIT许可证。

自从它被分叉以来,表单引擎代码已经完全重写,但它最初是基于Nibble Forms 2构建的,而Nibble Forms 2本身是Nibble Forms的分叉。