承接 sndsgd/form 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

sndsgd/form

Composer 安装命令:

composer require sndsgd/form

包简介

Structured data validation for PHP

README 文档

README

Latest Version Software License Build Status Coverage Status Total Downloads

Structured data validation for PHP.

Requirements

This project is unstable and subject to changes from release to release.

You need PHP >= 7.0 to use this library, however, the latest stable version of PHP is recommended.

Install

Install sndsgd/form using Composer.

composer require sndsgd/form

Usage

Define a form

<?php

use \sndsgd\form\field;
use \sndsgd\form\rule;

require __DIR__."/../vendor/autoload.php";

$form = (new \sndsgd\Form())
    ->addFields(
        (new field\StringField("name"))
            ->setDescription(_("The user's name"))
            ->addRules(
                new rule\RequiredRule()
            ),
        (new field\StringField("email"))
            ->setDescription(_("The user's email address"))
            ->addRules(
                new rule\RequiredRule(),
                new rule\EmailRule()
            )
    );

Validate input

$inputValues = [
    "name" => "",
    "email" => "asd@asd.com",
    "whoami" => "'whoami' should be unexpected",
];

$validator = new \sndsgd\form\Validator($form);
try {
    $values = $validator->validate($inputValues);
    $message = "all values were valid";
} catch (\sndsgd\form\ValidationException $ex) {
    $message = "validation errors encountered";
    $errors = $ex->getErrors();
}

echo json_encode([
    "message" => $message,
    "data" => $data ?? null,
    "errors" => $errors ?? [],
], \sndsgd\Json::HUMAN);
{
    "message": "validation errors encountered",
    "data": null,
    "errors": [
        {
            "message": "required",
            "code": 0,
            "field": "name"
        },
        {
            "message": "unknown field",
            "code": 0,
            "field": "whoami"
        }
    ]
}

Get details for docs

$detail = $form->getDetail();
echo json_encode($detail, \sndsgd\Json::HUMAN);
[
    {
        "name": "name",
        "type": "string",
        "signature": "string",
        "description": "your name",
        "default": null,
        "rules": {
            "values": [
                {
                    "description": "required",
                    "errorMessage": "required"
                },
                {
                    "description": "email",
                    "errorMessage": "must be a valid email address"
                }
            ]
        }
    },
    {
        "name": "email",
        "type": "string",
        "signature": "string",
        "description": "your email address",
        "default": null,
        "rules": {
            "values": [
                {
                    "description": "required",
                    "errorMessage": "required"
                },
                {
                    "description": "email",
                    "errorMessage": "must be a valid email address"
                }
            ]
        }
    }
]

Field Types

Single Value Fields

\sndsgd\form\field\ValueField

This field type can be used to store a single value of various types. In most cases it'll be the base class for any custom fields that you want to create.

$ageField = (new field\ValueField("age"))
    ->setDescription(_("The user's age in years"))
    ->addRules(
        new rule\IntegerRule(),
        new rule\MinRule(0),
        new rule\MaxRule(150)
    );

We've created several subclasses of ValueField for commonly used types. Note that whenever you use one of these fields, you can skip adding the corresponding type rule.

  • \sndsgd\form\field\BooleanField
  • \sndsgd\form\field\FloatField
  • \sndsgd\form\field\IntegerField
  • \sndsgd\form\field\StringField

Multiple Value Fields

Whenever you need a field that will have multiple values, you'll need to define a field that will contain one or more fields for each value. This allows for each distinct value to be validated on its own, and then all values to be validated with each other.

\sndsgd\form\field\ArrayField

Use an ArrayField whenever you have a need for multiple values of the same type.

# this field defines what is expected for each value in the array
$nicknamesValue = (new field\StringField())
    ->addRules(
        new rule\MaxLengthRule(64),
        (new rule\RegexRule("/^[a-z0-9 ]+$/i"))
            ->setErrorMessage(_("must contain only letters, numbers, or spaces"))
    );

# create an array field as the parent of the value field
# note: the rules are for validating all values as a group; if you need to
# validate each value, add the relevant rules to the value field
$nicknamesField = (new field\ArrayField("nicknames", $nicknamesValue))
    ->setDescription(_("The user's nicknames"))
    ->addRules(
        new rule\MaxValueCountRule(5)
    );

\sndsgd\form\field\MapField

Use a MapField whenever you need to validate both the keys and values of an object.

# define what is expected for a key in the map
$phoneKey = (new field\StringField())
    ->setDescription(_("A label for a phone number"))
    ->addRule(new rule\AlphaNumRule());

# define what is expected for a value in the map
$phoneValue = (new field\StringField())
    ->setDescription(_("A phone number"))
    ->addRule(new rule\MinLengthRule(10));

# create a map field using the key and value fields
# as with the array field, any rules added to a map field are used for
# validating all the values as a group
$phonesField = (new field\MapField("phoneNumbers", $phoneKey, $phoneValue))
    ->setDescription(_("The user's phone numbers"))
    ->addRule(new rule\MaxValueCountRule(5));

\sndsgd\form\field\ObjectField

Use ObjectField whenever you need to validate only the values of an object. It'll use the name of any fields you add to it as the keys.

$field = (new field\ObjectField())
    ->addFields(
        $ageField,
        $nicknamesField,
        $phonesField
    );

Documentation

$detail = $field->getDetail();
echo json_encode($detail, \sndsgd\Json::HUMAN);
[
    {
        "name": "age",
        "type": "int",
        "signature": "int",
        "description": "The user's age in years",
        "default": null,
        "rules": {
            "values": [
                {
                    "description": "type:integer",
                    "errorMessage": "must be an integer"
                },
                {
                    "description": "min:0",
                    "errorMessage": "must be at least 0"
                },
                {
                    "description": "max:150",
                    "errorMessage": "must be no greater than 150"
                }
            ]
        }
    },
    {
        "name": "nicknames",
        "type": "array",
        "signature": "array<string>",
        "description": "The user's nicknames",
        "default": [],
        "rules": {
            "values": [
                {
                    "description": "max-length:64",
                    "errorMessage": "must be no more than 64 characters"
                },
                {
                    "description": "regex:/^[a-z0-9 ]+$/i",
                    "errorMessage": "must contain only letters, numbers, or spaces"
                },
                {
                    "description": "max-values:5",
                    "errorMessage": "must be no more than 5 values"
                }
            ]
        }
    },
    {
        "name": "phoneNumbers",
        "type": "map",
        "signature": "map<string,string>",
        "description": "The user's phone numbers",
        "default": null,
        "rules": {
            "keys": [
                {
                    "description": "alphanumeric",
                    "errorMessage": "must contain only alphanumeric characters"
                }
            ],
            "values": [
                {
                    "description": "min-length:10",
                    "errorMessage": "must be at least 10 characters"
                },
                {
                    "description": "max-values:5",
                    "errorMessage": "must be no more than 5 values"
                }
            ]
        }
    }
]

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 1.02k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 16
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-03-29