selcukmart / form-generator
Modern PHP Form Generator with Chain Pattern - Supports Symfony, Laravel, Twig, and Smarty
Installs: 7
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 1
Open Issues: 0
pkg:composer/selcukmart/form-generator
Requires
- php: >=8.1
- ext-iconv: *
- ext-json: *
- ext-mbstring: *
- smarty/smarty: ^4.0|^5.0
- symfony/http-foundation: ^6.0|^7.0
- twig/twig: ^3.0
Requires (Dev)
- doctrine/orm: ^2.0|^3.0
- friendsofphp/php-cs-fixer: ^3.0
- illuminate/support: ^10.0|^11.0
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.0|^11.0
- roave/security-advisories: dev-latest
- symfony/form: ^6.0|^7.0
Suggests
- doctrine/orm: For Doctrine ORM data provider
- illuminate/database: For Laravel Eloquent data provider
- symfony/form: For Symfony Form integration
- dev-master
- 3.2.1
- 3.2.0
- 3.0.0
- 2.3
- 2.2
- 2.1
- 2.0
- 1.02
- 1.0.1
- 1.0.0
- dev-claude/update-documentation-011CUZKqR6bHuxuHatvedSit
- dev-claude/symfony-alternative-complete-011CUXv2h6nBNdGjJMXxRmow
- dev-claude/enhance-form-generator-011CUXpdFjpDVCvaxXmitSrL
- dev-claude/symfony-data-transform-011CUXoWtHSnFux79gZ2yqLV
- dev-claude/integrate-symfony-events-011CUXgUaP3y6w9dBiP7dv9X
- dev-claude/analyze-library-011CUQXXa4xmhDmpPdQ7LSLd
This package is auto-updated.
Last update: 2025-10-28 15:05:27 UTC
README
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.
Version 3.0.0 - Production Ready
FormGenerator V3.0.0 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 V3 Documentation β
Quick Start - Simple Contact Form with i18n & CSRF
use FormGenerator\V2\Builder\FormBuilder; use FormGenerator\V2\Translation\FormTranslator; use FormGenerator\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 FormGenerator\V2\Builder\FormBuilder; use FormGenerator\V2\Form\Form; use FormGenerator\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 FormGenerator\V2\Builder\FormBuilder; use FormGenerator\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/form-generator
π§ͺ 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 (FormGenerator)
$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
- Complete V3 Documentation - Full feature guide β
- V2 Documentation - Legacy V2 documentation
- Symfony Alternative Guide - Migration from Symfony Forms
- Upgrade Guide - Version migration guides
- Changelog - Version history
- Contributing - Contribution guidelines
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
- Data Transformation (v2.3.1)
- Event-Driven Dependencies (v2.3.0)
- Laravel-Style Validation (v2.1.0)
- Form Wizard/Stepper
- Built-in Pickers
- Doctrine Integration
- Laravel Integration
π€ 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
- Email: admin@hostingdevi.com
- GitHub: @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