Skip to content

Create Symfony forms using PHP attributes to define forms, fields, transformers and event listeners directly in your DTO classes

License

Notifications You must be signed in to change notification settings

Kenny1911/symfony-attribute-form

Repository files navigation

Symfony Attribute Form

[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 name
  • options - form options

#[Field]

Defines a form field. Can be applied to class properties:

  • type - field type (default: TextType)
  • label - field label
  • required - 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 name
  • listener - callable listener
  • priority - priority

Requirements

  • PHP 8.1+
  • Symfony Form 6.4+

License

MIT

About

Create Symfony forms using PHP attributes to define forms, fields, transformers and event listeners directly in your DTO classes

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages