Ordered Form Bundle

The Becklyn Ordered Form Bundle provides a configurable "position" option for all Symfony form field types.

Installation

  1. First install the bundle via composer:

    composer require becklyn/ordered-form-bundle
  2. Load the bundle in your bundles.php.

    <?php
    
    return [
        // ...
        Becklyn\OrderedFormBundle\BecklynOrderedFormBundle::class => ['all' => true],
        // ...
    ];

Usage

The newly added "position" is optional and can be set inside of any form field type’s options array. Its value can either be an absolute or a relative to another form field. To position a form field using an absolute position, the value can either be an integer (ordered ascending) or one of two string values ("first" or "last"). To sort fields relative to others, "position"``s value must be set to an ``associative array, whose key is either before or after, and its value the name of the referenced field.

With those conditions a buildForm-method of a form could look like this:

<?php

public function buildForm (FormBuilderInterface $builder, array $options) : void
{
    $builder
        ->add("firstName", null, [
            "label" => "First Name",
            "position" => "first",
        ])
        ->add("gender", null, [
            "label" => "Gender",
            "required" => false,
            "position" => "last",
        ])
        ->add("lastName", null, [
            "label" => "Last Name",
            "position" => ["after" => "firstName"],
        ])
        ->add("street", null, [
            "label" => "Street",
            "position" => 2,
        ])
        ->add("city", null, [
            "label" => "City",
            "position" => 3,
        ]);

    if (true)
    {
        $builder
            ->add("title", null, [
                "label" => "Formal Title",
                "required" => false,
                "position" => ["before" => "gender"],
            ]);
    }
}
Table of Contents