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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 small/forms 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The bundle for easy using json-rpc api on your project
Diese Contao 4 Erweiterung stellt Google reCAPTCHA V2 in Form eines neuen Formularfeldes im Formulargenerator bereit. This extension provides Google reCAPTCHA V2 in the form of a new form field in the form generator of Contao Open Source CMS.
A Laravel Filament Forms slug field.
Adds request-parameter validation to the SLIM 3.x PHP framework
Bundle Symfony DaplosBundle
A jQuery augmented PHP library for creating and validating HTML forms
统计信息
- 总下载量: 204
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 17
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-04-27