Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

[2.1] [Forms] [Bug] Factory instantiates Elements directly but should be using the FormElementManager #3555

Closed
netiul opened this Issue · 0 comments

2 participants

@netiul

Due to wrong approach inside the Form Factory, Elements unknown to the FormElementManager (FEM) are directly instantiated while they actually should be auto added as invokables and then created by the FEM.

You will get in trouble if that Element is an instance or a subclass of Fieldset since you most likely will need the Form Factory with the FEM in there too. Normally this is taken care of by the FEM, which has registered a listener that injects a Form Factory (with the FEM attached) into any Element implementing FormFactoryAwareInterface.

But since the Factory first does a has-check on the FEM an unknown Element will not be auto-added as invokable, as you can see here:
Form Factory

protected function getElementFromName($elementName)
{
    $formElementManager = $this->getFormElementManager();

    if ($formElementManager->has($elementName)) {
        return $formElementManager->get($elementName);
    }

    return new $elementName();
}

If you for example add a Collection by using the FQCN as type, the FEM will not be available inside the Collection (instance of Fieldset) since Zend\Form\Element\Collection is not known to the FEM and therefore directly instantiated by the Factory and no Factory with attached FEM is injected. As a side effect a specified hydrator cannot be pulled from the ServiceLocator too.

My suggestion is to change the above function to always call get, to let the FEM take care and auto add the Element as an invokable when not registered.

protected function getElementFromName($elementName)
{
    $formElementManager = $this->getFormElementManager();
    return $formElementManager->get($elementName);
}

Edit:
I propose to completely remove getElementFromName since it just proxies to the FormElementManager.

@netiul netiul referenced this issue from a commit in netiul/zf2
@netiul netiul fix for zendframework/zf2#3555 35d37a5
@weierophinney weierophinney referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
@ghost Unknown referenced this issue from a commit
@netiul netiul fix for zendframework/zf2#3555 f490903
@ghost Unknown referenced this issue from a commit
@weierophinney weierophinney Merge branch 'hotfix/3556' into develop
Close #3556
Fixes #3555
e7f7304
@francisbesset francisbesset referenced this issue from a commit in zf2-split/Component_ZendForm
@netiul netiul fix for zendframework/zf2#3555 53da4f7
@fabiocarneiro fabiocarneiro referenced this issue from a commit in fabiocarneiro/zend-form
@netiul netiul fix for zendframework/zf2#3555 d627140
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.