letkode/form-options-bundle

Attribute-driven form options registry and endpoint for Symfony applications

Maintainers

Package info

github.com/letkode/form-options-bundle

Type:symfony-bundle

pkg:composer/letkode/form-options-bundle

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.0 2026-06-19 19:56 UTC

This package is auto-updated.

Last update: 2026-06-19 20:04:05 UTC


README

Attribute-driven form options registry and endpoint for Symfony applications.

Allows any service to expose named option sets via a #[FormOption] PHP Attribute, which are then discoverable at runtime through a single REST endpoint.

Installation

composer require letkode/form-options-bundle

Symfony Flex will register the bundle automatically. If not using Flex, add it manually:

// config/bundles.php
return [
    Letkode\FormOptionsBundle\LetkodeFormOptionsBundle::class => ['all' => true],
];

Import the routes to expose the endpoint:

# config/routes/letkode_form_options.yaml
letkode_form_options:
    resource: '@LetkodeFormOptionsBundle/config/routes.yaml'

Usage

1. Mark a service as a provider

use Letkode\FormOptionsBundle\Attribute\FormOption;
use Letkode\FormOptionsBundle\Attribute\FormOptionMethod;

#[FormOption(alias: 'countries')]
final class CountryRepository extends ServiceEntityRepository
{
    #[FormOptionMethod(alias: 'active')]
    public function findActiveAsOptions(): array
    {
        return $this->createQueryBuilder('c')
            ->select('c.uuid, c.code, c.name')
            ->where('c.active = true')
            ->getQuery()
            ->getArrayResult();
    }
}

2. Fetch options via the API

GET /api/form-options/countries/active
[
    { "uuid": "...", "code": "CL", "name": "Chile" },
    { "uuid": "...", "code": "AR", "name": "Argentina" }
]

How it works

  • LetkodeFormOptionsBundle::build() calls registerAttributeForAutoconfiguration() so any service annotated with #[FormOption] is automatically tagged as letkode.form_options.provider.
  • FormOptionRegistry collects all tagged providers via #[AutowireIterator].
  • FormOptionsController resolves provider + method by alias and returns the result as JSON.

Requirements

  • PHP ^8.4
  • Symfony ^7.0 || ^8.0

License

MIT — see LICENSE.