Fixes #3 - Element::init is called before Element::setOptions when using Factory but not when using FormElementManager#251
Conversation
…when using Factory but not when using FormElementManager Signed-off-by: Frank Brückner <dev@froschdesignstudio.de>
|
Hold off merging this. Implications need to be understood. I remember that creating form via spec is not compatible with custom forms (or elements) that rely on init for self-configuration. Form configuration via spec is meant for generic forms and those do not have |
|
@Xerkus |
|
laminas-servicemanager plugin manager provides One significant difference of Overall change here makes sense. I am rather inclined to treat this as a new feature rather than a bug fix. Options are already set as they meant to. I am conflicted about this somewhat because of a surprise factor. Documentation is already pretty unclear or even misleading. With this change spec options become available for As a followup of slack discussion I tried to identify needed documentation improvements only some of which should be handled here:
|
| $type = $spec['type'] ?? Element::class; | ||
|
|
||
| $element = $this->getFormElementManager()->get($type); | ||
| $element = $this->getFormElementManager()->get($type, $spec['options'] ?? []); |
There was a problem hiding this comment.
Build parameters must be ['options' => $spec['options']] according to ElementFactory and if options are not set or empty build parameters should be null to not trigger plugin manager's build() behavior.
ElementFactory also accepts element name as build parameters, so we should probably pass it too if $spec has it.
Something along those lines:
| $element = $this->getFormElementManager()->get($type, $spec['options'] ?? []); | |
| $buildParams = null; | |
| if (! empty($spec['options'])) { | |
| $buildParams = ['options' => $spec['options']]; | |
| $name = $spec['name'] ?? null; | |
| if ($name !== null && $name !== '') { | |
| $buildParams['name'] = $name; | |
| } | |
| } | |
| $element = $this->getFormElementManager()->get($type, $buildParams); |
There was a problem hiding this comment.
note that options could be iterable, according to code. I did not handle it in suggested change.
Description
Fixes #3: Element::init is called before Element::setOptions when using Zend\Form\Factory but not when using FormElementManager