scafera/form
Composer 安装命令:
composer require scafera/form
包简介
Form handling and validation for the Scafera framework
关键字:
README 文档
README
Form handling and validation for the Scafera framework. DTO-based forms with attribute validation, CSRF protection, and typed value handling.
Internally adopts symfony/form, symfony/validator, and symfony/security-csrf. Userland code never imports Symfony Form or Validator types — boundary enforcement blocks it at compile time.
Provides: Form handling and validation for Scafera — DTO classes with
Rule\*attributes, handled byFormHandler(web path, CSRF + old values) orValidator(standalone / API path). CSRF tokens scoped per DTO class (ADR-061). No Symfony FormType in userland (ADR-059).Depends on: A Scafera host project. For file uploads, compose with
scafera/filein the controller (ADR-062) — form and file are intentionally separate capabilities.Extension points:
- Attributes —
Rule\NotBlank,MaxLength,MinLength,Min,Max,Positive,OneOf,FileExtensionon DTO properties- Controlled zone —
src/Form/allows SymfonyAbstractTypeclasses for complex forms that exceed DTO capabilities (same pattern assrc/Repository/for Doctrine)- Supported DTO property types —
string,int,float,bool,DateTimeImmutable,BackedEnum, nullable variantsNot responsible for: File uploads (compose with
scafera/file, ADR-062) · form themes orform_widget()(plain HTML only) · persistence (form is input-only) · direct use ofSymfony\Component\FormorSymfony\Component\Validatoroutsidesrc/Form/(blocked byFormBoundaryPassandFormBoundaryValidator).
This is a capability package. It adds optional form handling and input validation to a Scafera project. It does not define folder structure or architectural rules — those belong to architecture packages.
What it provides
Validator— standalone DTO validation (API path)FormHandler— form creation, submission, CSRF, and validation in one flow (web path)Form— result object with errors, old values, and CSRF tokenRule\*attributes — declarative validation on DTO propertiesDtoHydrator— internal DTO mapping with type transformation (nullable, enum, DateTimeImmutable)src/Form/controlled zone — Symfony Form types for complex forms
Design decisions
- DTOs with attributes, not Symfony FormType — the common case uses plain PHP classes with
Rule\*attributes. No Symfony types in userland (ADR-059). - CSRF scoped per form identity — each DTO class gets its own CSRF token. A token from one form cannot submit another (ADR-061).
- File uploads handled separately — use
scafera/filealongsideFormHandlerin your controller (ADR-062). src/Form/is a controlled zone, not an escape hatch — Symfony Form types are allowed in this directory only, following the same pattern assrc/Repository/for Doctrine.- Type transformation on old values —
old()returns typed values (int, enum, null), not raw strings. Templates render correctly without manual casting.
Installation
composer require scafera/form
Requirements
- PHP >= 8.4
- scafera/kernel
Input DTO
use Scafera\Form\Rule; final class CreatePostInput { #[Rule\NotBlank] #[Rule\MaxLength(200)] public string $title = ''; #[Rule\MaxLength(5000)] public ?string $body = null; #[Rule\Positive] public int $priority = 1; }
Supported property types: string, int, float, bool, DateTimeImmutable, nullable variants, and BackedEnum.
Available rules
| Rule | Parameters |
|---|---|
#[NotBlank] |
— |
#[MaxLength(int)] |
max characters |
#[MinLength(int)] |
min characters |
#[Min(int|float)] |
minimum value |
#[Max(int|float)] |
maximum value |
#[Positive] |
must be > 0 |
#[OneOf(array)] |
allowed values |
#[Email] |
email format |
#[FileExtension(array)] |
allowed file extensions |
Standalone validation (API path)
use Scafera\Form\Validator; $input = new CreatePostInput(); $input->title = ''; $result = $validator->validate($input); $result->hasErrors(); // true $result->firstError(); // ValidationError{field: 'title', message: '...'} $result->toArray(); // ['title' => ['This value should not be blank.']]
Form handling (web path)
use Scafera\Form\FormHandler; #[Route('/posts/new', methods: ['GET', 'POST'])] final class CreatePost { public function __construct( private readonly FormHandler $form, private readonly ViewInterface $view, ) {} public function __invoke(Request $request): ResponseInterface { $form = $this->form->handle($request, CreatePostInput::class); if ($form->isValid()) { $input = $form->getData(); // typed CreatePostInput // persist... return new RedirectResponse('/posts'); } return new Response($this->view->render('posts/new.html.twig', [ 'form' => $form, ])); } }
Template
Plain HTML. No form themes, no form_widget():
<form method="POST"> <input type="hidden" name="_csrf" value="{{ form.csrfToken() }}"> <label>Title</label> <input name="title" value="{{ form.old('title') }}"> {% if form.error('title') %} <span class="error">{{ form.error('title') }}</span> {% endif %} <button type="submit">Create</button> </form>
Form API
| Method | Returns |
|---|---|
isSubmitted() |
bool |
isValid() |
bool (true only when submitted + valid) |
getData() |
typed input DTO |
errors() |
array<string, list<string>> |
error(string $field) |
?string (first error for field) |
old(string $field) |
mixed (submitted value, type-transformed) |
csrfToken() |
string |
Controlled zone: src/Form/
For forms that exceed DTO capabilities (nested forms, dynamic fields, entity-backed choices), place Symfony AbstractType classes in src/Form/:
src/
Form/ ← Symfony Form imports allowed here
OrderForm.php
This is a controlled zone — the same pattern as src/Repository/ for Doctrine. Symfony Form and Validator imports are allowed in this directory and blocked everywhere else.
Rules for src/Form/:
- May reference
App\Entityfor type hints and structure — must NOT perform persistence or lifecycle operations - Must NOT import from
App\Service,App\Repository,App\Controller,App\Command, orApp\Integration - Only controllers may import from
App\Form— services must NOT depend on forms - You are responsible for wiring Form classes into your controller via Symfony's
FormFactorydirectly
Boundary enforcement
| Blocked | Use instead |
|---|---|
Symfony\Component\Form\* |
Scafera\Form\FormHandler |
Symfony\Component\Validator\* |
Scafera\Form\Validator + Rule\* |
Allowed inside src/Form/ only (controlled zone). Enforced via compiler pass (build time) and validator (scafera validate). Detects use, new, and extends patterns.
License
MIT
scafera/form 适用场景与选型建议
scafera/form 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「form」 「validation」 「scafera」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 scafera/form 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 scafera/form 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 scafera/form 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
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
A jQuery augmented PHP library for creating and validating HTML forms
A Laravel validator for delimiter-separated list of emails.
A CakePHP behavior to validate foreign keys
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 29
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-04-14