kenny1911 / symfony-attribute-form
Create Symfony forms using PHP attributes to define forms, fields, transformers and event listeners directly in your DTO classes
Installs: 2
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/kenny1911/symfony-attribute-form
Requires
- php: ^8.1
- symfony/form: ^6.4 || ^7.0
Requires (Dev)
- phpunit/phpunit: ^12.4
This package is auto-updated.
Last update: 2025-11-10 21:06:36 UTC
README
[English][Русский]
Description
A library for creating Symfony forms using PHP attributes. Allows defining forms, their fields, transformers, and event listeners directly in DTO classes through attributes.
Installation
composer require kenny1911/symfony-attribute-form
Usage
Defining Form Class
<?php declare(strict_types=1); use Kenny1911\SymfonyAttributeForm\EventListener; use Kenny1911\SymfonyAttributeForm\Field; use Kenny1911\SymfonyAttributeForm\Form; use Kenny1911\SymfonyAttributeForm\ModelTransformer; use Kenny1911\SymfonyAttributeForm\ViewTransformer; use Symfony\Component\Form\Extension\Core\Type\DateTimeType; use Symfony\Component\Form\Extension\Core\Type\TextareaType; use Symfony\Component\Form\FormEvents; #[Form(name: 'create-post', options: ['action' => '/posts/new'])] #[ModelTransformer(new NothingDataTransformer())] #[ViewTransformer(new NothingDataTransformer())] #[EventListener(eventName: FormEvents::PRE_SUBMIT, listener: [self::class, 'preSubmitForm'], priority: 5)] final class CreatePost { public function __construct( #[Field(label: 'Post title', options: ['attr' => ['class' => 'input']])] public ?string $title = null, #[Field(type: TextareaType::class, label: 'Post description', required: true, options: ['attr' => ['class' => 'textarea']])] #[ModelTransformer(new NothingDataTransformer())] #[ViewTransformer(new NothingDataTransformer())] #[EventListener(eventName: FormEvents::PRE_SET_DATA, listener: [self::class, 'preSetDescriptionData'], priority: 7)] public ?string $description = null, #[Field(type: DateTimeType::class, label: 'Published date', required: false, options: ['attr' => ['class' => 'input']])] public ?\DateTimeImmutable $published = null, ) {} public static function preSubmitForm(): void { // Code for PRE_SUBMIT event handling } public static function preSetDescriptionData(): void { // Code for PRE_SET_DATA event handling for description field } }
Creating Form
$form = $this->attributeFormFactory->createFormBuilder(CreatePost::class);
Symfony Framework Integration
# config/services.yaml services: Kenny1911\SymfonyAttributeForm\AttributeFormFactory: arguments: - '@form.factory'
Available Attributes
#[Form]
Defines the main form:
name- form nameoptions- form options
#[Field]
Defines a form field. Can be applied to class properties:
type- field type (default:TextType)label- field labelrequired- required field (default:true)options- additional field options
#[ModelTransformer] and #[ViewTransformer]
Add data transformers to form or field.
#[EventListener]
Adds event listener to form or field:
eventName- event namelistener- callable listenerpriority- priority
Requirements
- PHP 8.1+
- Symfony Form 6.4+
License
MIT