chamber-orchestra/form-bundle 问题修复 & 功能扩展

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

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

chamber-orchestra/form-bundle

Composer 安装命令:

composer require chamber-orchestra/form-bundle

包简介

Symfony 8 bundle for JSON-first API form handling with controller traits, data transformers, Doctrine validation, and RFC 9457 problem details error responses

README 文档

README

PHP Composer Latest Stable Version License PHP 8.5 Symfony 8 PHPStan Max PHP-CS-Fixer Code Style: PER-CS & Symfony

ChamberOrchestra Form Bundle

A Symfony 8 bundle that simplifies JSON-first form handling for REST APIs. It provides controller traits for the submit/validate/response flow, specialized API form types with CSRF disabled, reusable data transformers, Doctrine-backed uniqueness validation, and structured error responses following RFC 9457 (Problem Details for HTTP APIs).

Features

  • Controller traits (FormTrait, ApiFormTrait) for submit/validate/response flow with null-safe request handling
  • JSON payload handling for mutation requests with automatic merging of uploaded files
  • API form base types (QueryForm, MutationForm) with CSRF disabled and empty block prefixes for clean JSON payloads
  • Custom form types: BooleanType, TimestampType, HiddenEntityType with secure query builder parameterization
  • Data transformers for booleans, Unix timestamps, comma-separated arrays, and JSON strings
  • RFC 9457 problem details via ValidationFailedView with structured violations for consistent API error responses
  • Translatable error normalizer (ProblemNormalizer) for localized exception messages in problem detail responses
  • UniqueField validation constraint for Doctrine repositories with multi-field checks, closure-based exclusions, and custom normalizers
  • TelExtension to strip non-digit characters from phone number input
  • CollectionUtils for syncing Doctrine collections (add new / remove stale items)

Requirements

  • PHP ^8.5
  • Symfony 8.0 components (framework-bundle, form, validator, config, dependency-injection, runtime, translation, clock)
  • Doctrine ORM 3.6
  • chamber-orchestra/view-bundle 8.0

Installation

composer require chamber-orchestra/form-bundle:8.0.*

Enable the bundle in config/bundles.php:

return [
    // ...
    ChamberOrchestra\FormBundle\ChamberOrchestraFormBundle::class => ['all' => true],
];

Usage

Controller Traits

Use FormTrait for standard HTML form submissions and ApiFormTrait for JSON API endpoints.

handleFormCall() accepts a form class or instance, handles the request, and returns a view or response. handleApiCall() does the same for API endpoints -- it automatically parses JSON payloads for MutationForm types and merges uploaded files.

onFormSubmitted() checks validity, returns a ValidationFailedView on failure, or invokes the callback on success:

use ChamberOrchestra\FormBundle\ApiFormTrait;
use ChamberOrchestra\ViewBundle\View\ViewInterface;
use Symfony\Component\HttpFoundation\Request;

final class SearchCourseAction
{
    use ApiFormTrait;

    public function __invoke(Request $request): ViewInterface
    {
        $form = $this->createForm(SearchCourseForm::class);
        $form->submit($request->query->all());

        return $this->onFormSubmitted($form, function (SearchCourseData $dto) use ($request) {
            $entities = $this->er->searchCourses(
                $pagination = $this->getPagination(['per_page_limit' => $this->getPerPageLimit($request)]),
                $dto->query,
                $dto->brands,
                $dto->topics,
                $dto->products,
                $dto->durations
            );

            return new PaginatedView($entities, $pagination, CourseView::class);
        });
    }
}

API Form Types

Extend QueryForm for GET requests or MutationForm for POST/PUT/PATCH requests. Both disable CSRF protection and use empty block prefixes for clean JSON input/output.

Data Transformers

Transformer Description
TextToBoolTransformer Converts "true", "1", "yes" to boolean
DateTimeToNumberTransformer Converts Unix timestamps to DateTimeInterface objects
ArrayToStringTransformer Converts arrays to/from comma-separated strings
JsonStringToArrayTransformer Parses JSON strings to arrays (handles empty strings)

HiddenEntityType

Loads Doctrine entities by ID from a hidden form field. Supports a custom query_builder option with secure parameterized queries.

UniqueField Validator

Validates field uniqueness against Doctrine repositories. Supports multiple fields, closure-based exclusions, custom normalizers, and targeted error paths.

ProblemNormalizer

Extends Symfony's ProblemNormalizer to translate exception messages when the exception implements TranslatableExceptionInterface. This ensures localized error messages in RFC 9457 problem detail responses.

Response Views

View HTTP Status Description
SuccessView 200 Empty success response
ValidationFailedView 422 Form validation errors with structured ViolationView items
FailureView Configurable Generic error response
RedirectView 301/302 Redirect response for AJAX requests
SuccessHtmlView 200 HTML fragment response for AJAX requests

Testing

Install dependencies and run the full test suite:

composer install
./vendor/bin/phpunit

The integration test kernel (tests/Integrational/TestKernel.php) boots a minimal Symfony application with in-memory SQLite for Doctrine tests.

License

MIT

chamber-orchestra/form-bundle 适用场景与选型建议

chamber-orchestra/form-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.99k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 12 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-30