定制 selcukmart/spider-form 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

selcukmart/spider-form

Composer 安装命令:

composer require selcukmart/spider-form

包简介

SpiderForm - Modern PHP Form Builder with Chain Pattern. Weaving perfect forms for Symfony & Laravel

README 文档

README

PHP Version License Version Tests

Modern PHP Form Generator

A production-ready form builder library offering nested forms, type system, cross-field validation, dynamic form modification, advanced error handling, internationalization, and automatic CSRF protection.

Production Ready

SpiderForm is a comprehensive form building library with an intuitive fluent API.

Key Features

Enterprise-Grade Form Builder

  • Comprehensive form building capabilities
  • Fluent chain pattern API
  • Framework-agnostic design (standalone or integrate with Symfony/Laravel)
  • 500+ comprehensive unit tests

🌍 Internationalization (NEW in v3.0.0)

  • Multi-language form labels and messages
  • Built-in translator with PHP and YAML loaders
  • Parameter interpolation ({{ param }} syntax)
  • Locale fallback chains

🔒 Automatic CSRF Protection (NEW in v3.0.0)

  • Session-based token management
  • Automatic token generation and validation
  • Configurable token lifetime (default: 2 hours)
  • Zero configuration required

📖 Read Full Documentation →

Quick Start - Simple Contact Form with i18n & CSRF

use SpiderForm\V2\Builder\FormBuilder;
use SpiderForm\V2\Translation\FormTranslator;
use SpiderForm\V2\Translation\Loader\PhpLoader;

// Setup translator (optional)
$translator = new FormTranslator('en_US');
$translator->addLoader('php', new PhpLoader());
$translator->loadTranslationFile(__DIR__ . '/translations/forms.en_US.php', 'en_US', 'php');

FormBuilder::setTranslator($translator);

// Build form with CSRF protection (automatic!)
$form = FormBuilder::create('contact_form')
    ->setAction('/contact/send')
    ->setMethod('POST')
    ->setCsrfTokenId('contact_form') // Automatic CSRF protection

    ->addText('name', 'form.label.name') // Translated automatically
        ->required()
        ->minLength(3)
        ->add()

    ->addEmail('email', 'form.label.email')
        ->required()
        ->add()

    ->addTextarea('message', 'form.label.message')
        ->required()
        ->minLength(20)
        ->add()

    ->addSubmit('send', 'form.button.send')
    ->build();

// Render with automatic CSRF token
echo $form;

Nested Forms with Cross-Field Validation

use SpiderForm\V2\Builder\FormBuilder;
use SpiderForm\V2\Form\Form;
use SpiderForm\V2\Validation\Constraints\Callback;

// Create address sub-form
$addressForm = new Form('address');
$addressForm->add('street', FormBuilder::text('street', 'Street'));
$addressForm->add('city', FormBuilder::text('city', 'City'));
$addressForm->add('zipcode', FormBuilder::text('zipcode', 'ZIP Code'));

// Create user form with nested address
$form = FormBuilder::create('user_registration')
    ->setCsrfTokenId('register')

    ->addText('username', 'Username')
        ->required()
        ->minLength(3)
        ->add()

    ->addPassword('password', 'Password')
        ->required()
        ->minLength(8)
        ->add()

    ->addPassword('password_confirm', 'Confirm Password')
        ->required()
        ->add()

    ->add('address', $addressForm) // Nested form!

    ->build();

// Add cross-field validation
$form->addConstraint(new Callback(function($data, $context) {
    if ($data['password'] !== $data['password_confirm']) {
        $context->buildViolation('Passwords do not match')
                ->atPath('password_confirm')
                ->addViolation();
    }
}));

// Validate and handle errors
$form->submit($_POST);
if ($form->isValid()) {
    // Process data
} else {
    $errors = $form->getErrorList(deep: true);
}

Dynamic Form Modification with Events

use SpiderForm\V2\Builder\FormBuilder;
use SpiderForm\V2\Event\FormEvents;

$form = FormBuilder::create('product_form')
    ->setCsrfTokenId('product')

    ->addSelect('product_type', 'Product Type')
        ->options([
            'physical' => 'Physical Product',
            'digital' => 'Digital Product',
        ])
        ->add()

    ->build();

// Add fields dynamically based on product type
$form->addEventListener(FormEvents::PRE_SET_DATA, function($event) {
    $data = $event->getData();
    $form = $event->getForm();

    if ($data['product_type'] === 'physical') {
        // Add shipping fields
        $form->add('weight', FormBuilder::number('weight', 'Weight (kg)'));
        $form->add('dimensions', FormBuilder::text('dimensions', 'Dimensions'));
    } else {
        // Add download fields
        $form->add('file_size', FormBuilder::text('file_size', 'File Size'));
        $form->add('download_limit', FormBuilder::number('download_limit', 'Downloads'));
    }
});

📊 Complete Feature Set

v3.0.0 - Internationalization & CSRF (Current)

  • 🌍 Multi-language support with built-in translator
  • 🔒 Automatic CSRF protection
  • 📝 Multiple translation loaders (PHP, YAML)
  • 🔄 Parameter interpolation in messages

v2.9.0 - Advanced Error Handling

  • ⚠️ Three error levels (ERROR, WARNING, INFO)
  • 🎯 Error bubbling from nested forms
  • 📋 Rich error metadata
  • 🔍 Error filtering and grouping

v2.8.0 - Dynamic Form Modification

  • 🎭 Event-driven form building
  • 🔄 Modify forms based on data
  • ➕ Add/remove fields dynamically
  • 🎬 Full event lifecycle

v2.7.0 - Cross-Field Validation

  • ✅ Field relationship validation
  • 🎯 Validation groups
  • 📝 Custom callback constraints
  • 🔧 Execution context

v2.5.0 - Type System

  • 🏗️ Custom field types
  • 🎨 Type inheritance
  • 🔌 Type extensions
  • ⚙️ Options resolver

v2.4.0 - Nested Forms

  • 🌳 Unlimited nesting
  • 🔁 Form collections
  • 👨‍👩‍👧 Parent-child relationships
  • 🗺️ Data mapping

Earlier Features

  • 🔄 Data transformation (v2.3.1)
  • 🎯 Event-driven dependencies (v2.3.0)
  • ✅ Laravel-style validation (v2.1.0)
  • 🎨 Bootstrap 5 & Tailwind themes
  • 🔐 CSRF, XSS protection
  • 🚀 Symfony & Laravel integration

📦 Installation

composer require selcukmart/spider-form

🧪 Testing

500+ Comprehensive Unit Tests

# Run all tests
vendor/bin/phpunit

# Run specific test suite
vendor/bin/phpunit tests/V2/Type/          # Type system tests
vendor/bin/phpunit tests/V2/Validation/    # Validation tests
vendor/bin/phpunit tests/V2/Event/         # Event system tests
vendor/bin/phpunit tests/V2/Error/         # Error handling tests
vendor/bin/phpunit tests/V2/Translation/   # i18n tests
vendor/bin/phpunit tests/V2/Security/      # CSRF tests

# With coverage
vendor/bin/phpunit --coverage-html coverage/

Test Coverage:

  • ✅ 400+ unit tests
  • ✅ 100+ integration tests
  • ✅ Full API coverage
  • ✅ Edge case handling
  • ✅ Real-world scenarios

🔄 Migration from Symfony Forms

Before (Symfony Form Component)

$form = $this->createFormBuilder()
    ->add('username', TextType::class, [
        'required' => true,
        'constraints' => [new Length(['min' => 3])]
    ])
    ->add('email', EmailType::class)
    ->getForm();

After (SpiderForm)

$form = FormBuilder::create('user_form')
    ->addText('username')->required()->minLength(3)->add()
    ->addEmail('email')->required()->add()
    ->build();

Advantages:

  • 🎯 Chain pattern is more readable
  • 🚀 No need for type classes
  • 🔧 Less boilerplate code
  • 💡 Better IDE autocomplete
  • 🎨 Cleaner syntax

See SYMFONY_ALTERNATIVE_ROADMAP.md for complete migration guide.

📚 Complete Documentation

Core Documentation

Examples by Version

🌍 v3.0.0 - Internationalization & CSRF (NEW!)

⚠️ v2.9.0 - Error Handling

🎭 v2.8.0 - Dynamic Forms

✅ v2.7.0 - Cross-Field Validation

🏗️ v2.5.0 - Type System

🌳 v2.4.0 - Nested Forms

Earlier Features

🤝 Contributing

We welcome contributions! Please see CONTRIBUTING.md for guidelines.

Areas for contribution:

  • Additional field types
  • More translation loaders
  • Framework integrations
  • Theme development
  • Documentation improvements

📝 Version History

  • v3.0.0 (2025) - i18n & Auto CSRF - Current
  • v2.9.0 (2025) - Advanced Error Handling & Bubbling
  • v2.8.0 (2025) - Dynamic Form Modification
  • v2.7.0 (2024) - Cross-Field Validation & Groups
  • v2.5.0 (2024) - Type System & Extensions
  • v2.4.0 (2024) - Nested Forms & Collections
  • v2.3.1 (2024) - Data Transformation
  • v2.3.0 (2024) - Event-Driven Dependencies
  • v2.1.0 (2024) - Laravel-Style Validation
  • v2.0.0 (2024) - Complete rewrite with chain pattern

See CHANGELOG.md for details.

📄 License

MIT License - see LICENSE for details.

👨‍💻 Author

selcukmart

🌟 Star History

If this project helped you, please give it a ⭐ on GitHub!

Made with ❤️ using modern PHP 8.1+

Production-ready • Secure by Default • Internationalized • Framework-agnostic

selcukmart/spider-form 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-10-28