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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
webmozart opened this issue Oct 22, 2012 · 3 comments
Closed

[Form] Add more concise signature to FormInterface::add() #5806

webmozart opened this issue Oct 22, 2012 · 3 comments

Comments

@webmozart
Copy link
Contributor

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)
@mvrhov
Copy link
mvrhov commented Oct 22, 2012

I'm all 👍
The only problem with this is that PHP doesn't allow for method override. If you are going to use func_get_args then we are probably going to loose the IDE auto completion.

@stof
Copy link
Member
stof commented Oct 22, 2012

@mvrhov the FormBuilder does not use func_get_args, simply an optional second argument

@kadryjanek
Copy link

I really like the idea 👍
@mvrhov there is no need of overriding methods just simply check the instance of the first argument and remove FormInterFace
requirement.
@bschussek you can also add FromInterfeace::addField method which would do the same as FormBuilderInterface::add(). Both solutions are better than creating fields in Event Listeners now.

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

fabpot added a commit that referenced this issue Dec 18, 2012
This PR was merged into the master branch.

Commits
-------

19d8510 [Form] Improved Form::add() and FormBuilder::add() to accept integers as field names
fb71964 [Form] Added an alternative signature Form::add($name, $type, $options)

Discussion
----------

[Form] Added an alternative signature Form::add($name, $type, $options)

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: #5806
Todo: -
License of the code: MIT
Documentation PR: symfony/symfony-docs#2024

---------------------------------------------------------------------------

by bschussek at 2012-12-18T10:42:55Z

ping @fabpot
@fabpot fabpot closed this as completed Dec 18, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
381F
Development

No branches or pull requests

5 participants
0