Skip to content

ZeMarine/symfony-form-options-builder

 
 

Repository files navigation

version symfony ![Lines](https://img.shields.io/badge/code lines-6026-green.svg) Total Downloads SensionLabsInsight Scrutinizer

symfony-form-options-builder

It helps you writing your Symfony FormType, with some traits and methods to add fields in buildForm() instead of array with some mysterious keys, and other stuff.

Installation

Changelog 3.* (symfony/form ^3, so Symfony ^3)

Changelog 2.* (symfony/form ^2.3, so Symfony 2.3, 2.4, 2.5, 2.6, 2.7 and 2.8)

Changelog 1.* (Symfony ^2.3)

Object oriented FormType

FormType::buildForm() object oriented instead of array

Example :

namespace FooBundle\Form\Type;

use steevanb\SymfonyFormOptionsBuilder\OptionsBuilder\TextOptionsBuilder;
use steevanb\SymfonyFormOptionsBuilder\OptionsBuilder\EmailOptionsBuilder;
use Symfony\Component\Form\Extension\Core\Type\TextType;

class BarType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        # Since PHP 5.5, you can use FooClass::class
        $builder->add('field_text', TextType::class, TextOptionsBuilder::create()->asArray());

        # Since PHP 5.6, you can use the variadic syntax. asVariadic() parameter is field name.
        $builder->add(
            ...EmailOptionsBuilder::create()
                ->setRequired(false)
                ->setPlaceHolder('default@mail.com')
                ->setTrim(false)
                ->asVariadic('field_email')
        );
    }
}

More documentation

BlockPrefixTrait

Add getBlockPrefix(), to always return same syntax for form type getBlockPrefix() method : form_type_formtypeclassname

Example :

namespace FooBundle\Form\Type;

use steevanb\SymfonyFormOptionsBuilder\BlockPrefixTrait;

class BarType extends AbstractType
{
    # Use this trait to define getBlockPrefix() required method. It will return form_type_bar
    use BlockPrefixTrait;
}

About

[symfony/form] buildForm() with objects instead of array

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%