8000 [Form] Add more concise signature to FormInterface::add() · Issue #5806 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
[Form] Add more concise signature to FormInterface::add() #5806
Closed
@webmozart

Description

@webmozart

Right now, the addition of fields in event listeners is quite complicated, because FormInterface does not support the same concise syntax for adding fields like FormBuilderInterface does:

<?php
$formFactory = $builder->getFormFactory();
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($formFactory) {
    if (/* some condition */) {
        $event->getForm()->add($formFactory->createNamed('myfield', 'text'));
    } else {
        $event->getForm()->remove('myfield');
    }
});

This can be simplified by matching the signature of FormInterface::add() with FormBuilderInterface::add():

<?php
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
    if (/* some condition */) {
        $event->getForm()->add('myfield', 'text');
    } else {
        $event->getForm()->remove('myfield');
    }
});

The method, like FormBuilderInterface::add(), would then support both signatures:

  • add($name, $type, array $options = array())
  • add(FormInterface $child)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0