maplephp/form

开发高级、一致且安全的表单,同时也非常容易进行验证。

v1.0.2 2023-11-29 15:35 UTC

This package is auto-updated.

Last update: 2024-09-29 17:25:40 UTC


README

创建高级、一致且安全的表单和验证。

1. 初始化

use MaplePHP\Form\Fields;
use MaplePHP\Form\Examples\TestFormFields; // You should create you own template file for fields

$fields = new Fields(new TestFormFields());

建议您复制AbstractFormFields类,将其转换为常规类,重命名并扩展到实际的AbstractFormFields抽象类。然后您可以开始创建和添加您自己的自定义输入字段。

基本:您可以从字段模板快速创建一个字段

$fields->[FIELD_TYPE]->[ARG1]->[ARG2]->get(); FIELD_TYPE: Form\Templates\Fields中的方法名称 ARG: 可链式参数,如输入名称、字段属性、验证等。

echo $fields->text()->name("email")->label("Email address")->attr([
        "type" => "email", 
        "placeholder" => "Input your email..."
    ])->get();

高级

使用表单编译器创建高级一致表单和验证。在框架和大型应用程序中表现非常出色。

创建字段

[
	inputFieldName => [
		// Field config…
	],
	…
	…
]

字段配置

type (字符串)

期望定义的表单类型键 示例: text, textarea, date, select, checkbox, radio 等。 必需

label (字符串)

定义输入标签 示例: 电子邮件地址

description (字符串)

定义输入标签 示例: 我们需要您的电子邮件以…

attr (数组)

向字段添加HTML属性 示例:

[
	class => inp-email, 
	type => email,
	placeholder => Fill in the email
]

items (数组)

添加复选框、单选按钮或选择列表项。 示例:

[
	1 => Yes, 
	0 => No
]

对于select、checkbox和radio等字段类型是必需的。

validate (数组)

向表单字段添加验证 示例:

[
	length => [1, 200],
	!email => NULL
]

电子邮件键前的感叹号表示仅在填写时才会验证电子邮件,否则跳过或进行其他验证。

config (多维数组)

为自定义字段传递自定义数据。 示例:

[
	role => admin,
	user_id => 5212
]

示例

1. 使用数组创建表单

以下是以数组形式构建整个表单

$fields->add([
    "firstname" => [
        "type" => "text", // Set form type (input text or textarea and so on.)
        "label" => "First name",
        "validate" => [
            "length" => [1, 80]
        ]
    ],
    "lastname" => [
        "type" => "text",
        "label" => "Last name",
        "validate" => [
            "length" => [1, 120]
        ]
    ],
    "email" => [
        "type" => "text",
        "label" => "Email",
        "description" => "We need you email so that we can contact you.",
        "attr" => [
            "type" => "email",
            "placeholder" => "john.doe@hotmail.com"
        ],
        "validate" => [
            "length" => [1, 120],
            "!email" => NULL
        ]
    ],
    "nested,item1" => [
        "type" => "radio",
        "label" => "Question 1",
        "validate" => [
            "length" => [1],
        ],
        "items" => [
            1 => "Yes",
            0 => "No"
        ],
        "value" => 1 // Default value
    ],
    "nested,item2" => [
        "type" => "radio",
        "label" => "Question 2",
        "validate" => [
            "length" => [1],
        ],
        "items" => [
            1 => "Yes",
            0 => "No"
        ],
        "value" => 1 // Default value
    ],
    "message" => [
        "type" => "textarea",
        "label" => "Message",
        "validate" => [
            "length" => [0, 2000]
        ]
    ],
    "gdpr" => [
        "type" => "checkbox",
        //"label" => "GDPR",
        "validate" => [
            "length" => [1, 1],
            "!equal" => [1]
        ],
        "items" => [
            1 => "I accept that my data will be saved according to GDPR"
        ]
    ]
    
]);

2. 设置值(如果需要的话)

如果您从数据库(接受多维数组和对象)获取值

$fields->setValues([
    "firstname" => "John",
    "lastname" => "John",
    "nested" => [
        "item1" => 0,
        "item2" => 1,
    ]
]);

3. 构建表单

在读取或验证之前,您始终需要构建表单。

$fields->build();

4. 读取表单

现在您可以读取表单。

echo '<form action="index.php" method="post">';
echo $fields->getForm();
echo "</form>";

5. 验证表单

现在您可以读取表单。

use MaplePHP\Form\Validate;

$fields->build();
$validate = new Validate($fields, $_POST);
if($error = $validate->execute()) {
    // HAS ERROR --> 
	echo "<pre>";
    print_r($error);
    echo "</pre>";

} else {
	// SUCCESS -->
	// Return filtered request (will only return values for added input fields)
	$request = $validate->getRequest(); // Uprotected
}