承接 small/forms 相关项目开发

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

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

small/forms

Composer 安装命令:

composer require small/forms

包简介

Provide PHP class to validate api data.

README 文档

README

            

About

This project provides a data validation forms.

Useful to validate entry api data.

It is compatible with Symfony constraints annotations

Install

composer require small/forms

Inline forms

You can easily create a form inside your controller action.

For example, we want to check that this entry data match our controller action :

\Small\Forms\Form\FormBuilder::createInlineForm()
    ->addField(
        'name', 
        new \Small\Forms\Form\Field\Type\StringType(),
        [new \Small\Forms\ValidationRule\ValidateNumberCharsLessThan(256)]
    )->addField('age', new \Small\Forms\Form\Field\Type\IntType())
;

try {
    $form->fillFromSymfonyRequest($request)
        ->validate($messages = new \Small\Collection\Collection\StringCollection())
    ;
} catch (\Small\Forms\ValidationRule\Exception\ValidationFailException) {
    return new \Symfony\Component\HttpFoundation\JsonResponse($messages, \Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST);
}

$arrayData = $form->toArray();

Class forms

If you want to reuse your form, you can declare it as a class :

namespace App\Form;

class PersonForm extends \Small\Forms\Form\AbstractForm
{

    public function build(): self {
        
        $this->addField(
            'name', 
            new \Small\Forms\Form\Field\Type\StringType(),
            [new \Small\Forms\ValidationRule\ValidateNumberCharsLessThan(256)]
        );
        
        $this->addField('age', new \Small\Forms\Form\Field\Type\IntType());
        
    }
    
}

And simply use it in your controllers :

try {
    $form = \Small\Forms\Form\FormBuilder::createFromFormClass(App\Form\PersonForm::class)
        ->fillFromSymfonyRequest($request)
        ->validate($messages = new \Small\Collection\Collection\StringCollection())
    ;
} catch (\Small\Forms\ValidationRule\Exception\ValidationFailException) {
    return new \Symfony\Component\HttpFoundation\JsonResponse($messages, \Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST);
}

$arrayData = $form->toArray();

Standard objects with attributes

You can also create a form directly from object

namespace App\DTO;

use Small\Forms\Modifier\NullIfEmptyModifier;use Small\Forms\ValidationRule\ValidateNotEmpty;
use Small\Forms\ValidationRule\ValidateGreaterOrEqual;

class Person
{

    #[ValidateNotEmpty]
    private string $name;
    #[ValidateGreaterThanOrEqual(18)]
    #[NullIfEmptyModifier]
    private int $age;

}
namespace App\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class Employe
{
    
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column]
    #[Assert\NotBlank]
    private string $name;
    #[ORM\Column]
    private int $age;
    #[ORM\Column]
    private int $idService;
    
    #[ORM\ManyToOne(inversedBy: 'Service')]
    #[ORM\JoinColumn(name: "idService", referencedColumnName: "id", nullable: true)]
    private Service|null $service = null;

}
try {

    $form = \Small\Forms\Form\FormBuilder::createFromAdapter(
        new \Small\Forms\Adapter\AnnotationAdapter($person)
    )->validate($messages = new \Small\Collection\Collection\StringCollection())
    ->hydrate($employe = new App\Entity\Employe());

} catch (\Small\Forms\ValidationRule\Exception\ValidationFailException) {
    return new \Symfony\Component\HttpFoundation\JsonResponse($messages, \Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST);
}

$useCase->joinCompanyUseCase($employe);

$form->fillFromObject($employe)
    ->hydrate($person)
;

return new \Symfony\Component\HttpFoundation\JsonResponse($person);

The validation rules will be parsed from your entity class php attributes.

Symfony DTO or entities

You can also create a form from DTO object

namespace App\DTO;

use Symfony\Component\Validator\Constraints as Assert;

class Person
{

    #[Assert\NotBlank]
    private string $name;
    #[Assert\GreaterThanOrEqual(18)]
    private int $age;

}
namespace App\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class Employe
{
    
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column]
    #[Assert\NotBlank]
    private string $name;
    #[ORM\Column]
    private int $age;
    #[ORM\Column]
    private int $idService;
    
    #[ORM\ManyToOne(inversedBy: 'Service')]
    #[ORM\JoinColumn(name: "idService", referencedColumnName: "id", nullable: true)]
    private Service|null $service = null;

}
try {

    $form = \Small\Forms\Form\FormBuilder::createFromAdapter(
        new \Small\Forms\Adapter\SymfonyAnnotationAdapter($person)
    )->validate($messages = new \Small\Collection\Collection\StringCollection())
    ->hydrate($employe = new App\Entity\Employe());

} catch (\Small\Forms\ValidationRule\Exception\ValidationFailException) {
    return new \Symfony\Component\HttpFoundation\JsonResponse($messages, \Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST);
}

$useCase->joinCompanyUseCase($employe);

$form->fillFromObject($employe)
    ->hydrate($person)
;

return new \Symfony\Component\HttpFoundation\JsonResponse($person);

The validation rules will be parsed from your entity class php attributes.

List of validation rules

  • ValidateAtLeastOneOf
  • ValidateBoolean
  • ValidateCallback
  • ValidateChoice
  • ValidateCountLessOrEqualThan
  • ValidateCountGreaterThan
  • ValidateCountLessOrEqualThan
  • ValidateCountLessThan
  • ValidateDateTime
  • ValidateDecimal
  • ValidateDivisibleBy
  • ValidateEmail
  • ValidateString
  • ValidateStringArray
  • ValidateUnique
  • ValidateDateTime
  • ValidateDivisibleBy
  • ValidateEmail
  • ValidateEmpty
  • ValidateEqual
  • ValidateFloat
  • ValidateFloatArray
  • ValidateGreater
  • ValidateGreaterOrEqual
  • ValidateInt
  • ValidateIntArray
  • ValidateIsFalse
  • ValidateIsNull
  • ValidateIsTrue
  • ValidateJson
  • ValidateLess
  • ValidateLessOrEqual
  • ValidateMatchRegex
  • ValidateMixedArray
  • ValidateNegativeNumber
  • ValidateNotEmpty
  • ValidateNotEqual
  • ValidateNotMatchRegex
  • ValidateNotNull
  • ValidateNumberCharsBetween
  • ValidateNumberCharLessThan
  • ValidatePositiveNumber
  • ValidateRange
  • ValidateRequired
  • ValidateChoice
  • ValidateSequencialy
  • ValidateString
  • ValidateStringArray
  • ValidateUnique

List of modifiers

  • ArrayToCollectionModifier
  • ExplodeModifier
  • FalseIfEmptyModifier
  • FormBooleanToPhpModifier
  • ImplodeModifier
  • LTrimModifier
  • NullIfEmptyModifier
  • RoundModifier
  • RTrimModifier
  • StringToDateTimeImmutableModifier
  • StringToDateTimeModifier
  • SubStrModifier
  • ToLowerModifier
  • ToUpperModifier
  • TrimModifier
  • UcFirstModifier
  • UcWordsModifier

small/forms 适用场景与选型建议

small/forms 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 204 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 04 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 204
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 17
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-04-27