-
-
Notifications
You must be signed in to change notification settings - Fork 52
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
Fixes #3 - Element::init is called before Element::setOptions when using Factory but not when using FormElementManager #251
base: 3.17.x
Are you sure you want to change the base?
Conversation
…when using Factory but not when using FormElementManager Signed-off-by: Frank Brückner <[email protected]>
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:
|
@@ -107,7 +107,7 @@ public function create($spec): ElementInterface | |||
$spec = $this->validateSpecification($spec, __METHOD__); | |||
$type = $spec['type'] ?? Element::class; | |||
|
|||
$element = $this->getFormElementManager()->get($type); | |||
$element = $this->getFormElementManager()->get($type, $spec['options'] ?? []); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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