maplephp/form 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

maplephp/form

Composer 安装命令:

composer require maplephp/form

包简介

Develop advanced, consistent, and secure forms that are also very easy to validate.

README 文档

README

Create advanced, consistent and secure forms and validations.

1. Initiate

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

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

It is recommended that you make a copy off AbstractFormFields class, make it to a regualar class, rename it and extend it to the real AbstractFormFields abstract class. Then you can start making and adding your own custom input fields.

Basic: You can either quick create one field form the fields template

$fields->[FIELD_TYPE]->[ARG1]->[ARG2]->get(); FIELD_TYPE: Method name from Form\Templates\Fields ARG: Chainable arguments like input name, fields attributes, validations and so on.

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

Advance:

Use the form compiler for advance consistent form creation and validation. Works really great in frameworks and large applications.

Create fields

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

Field config

type (string)

Expects defined form type key Example: text, textarea, date, select, checkbox, radio and more. Required

label (string)

Define a input label Example: Email address

description (string)

Define a input label Example: We need your email to…

attr (array)

Add html attributens to field Example:

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

items (array)

Add checkbox, radio or select list items. Example:

[
	1 => Yes, 
	0 => No
]

Is required for field types like select, checkbox and radio.

validate (array)

Add validation to form field Example:

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

The exclamation point before the email key means that it will only validate email if it is filled in else skip or do the other validation.

config (multidimensional array)

Pass on custom data for a custom field. Example:

[
	role => admin,
	user_id => 5212
]

Examples:

1. Create form with array

Build a whole form with array as bellow

$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. Set values if you want

If you have values from for example the database (accepts multidimensional array and object)

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

3. Build the form

You will allways need to build the form before read or validations.

$fields->build();

4. Read form

Now you can read the form.

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

5. Validate form

Now you can read the form.

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
}

maplephp/form 适用场景与选型建议

maplephp/form 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 76 次下载、GitHub Stars 达 2, 最近一次更新时间为 2023 年 11 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「form」 「builder」 「form builder」 「validate」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 maplephp/form 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 maplephp/form 我们能提供哪些服务?
定制开发 / 二次开发

基于 maplephp/form 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 76
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 27
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2023-11-29