steevanb / symfony-form-options-builder
buildForm() with objects instead of array
Installs: 3 488
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 3
Open Issues: 13
Requires
- php: ^7.1||^8.0
- symfony/form: ^3.0||^4.0||^5.0||^6.0||^7.0
Requires (Dev)
- doctrine/orm: ^2.8
- symfony/doctrine-bridge: ^6.3
- symfony/security-csrf: ^6.3
This package is auto-updated.
Last update: 2024-12-09 09:46:56 UTC
README
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.
Object-oriented FormType
FormType::buildForm() object oriented instead of array
Example:
namespace FooBundle\Form\Type; use Steevanb\SymfonyFormOptionsBuilder\OptionsBuilder\EmailOptionsBuilder; use Steevanb\SymfonyFormOptionsBuilder\OptionsBuilder\TextOptionsBuilder; 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') ); } }
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; }