Enhanced form element specification #457
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Re: #453
All aspects of an element can be specified in one invocation. Consider this common scenario:
This can all be accomplished with one call:
This enhanced specification makes it easier to add additional attributes to take advantage of advanced client side features needed in rich web applications. It also simplifies dynamic form generation.
The associative array names are normally HTML attribute names. In some cases, these do not map directly or consistently. For example, Xoops\Form\Elements usually have a 'caption' attribute, but that is not part of the actual HTML input element. The 'value' attribute is usually a normal attribute, but in some cases, such as a TextArea, it is not rendered as an attribute in the tag, but as text enclosed in the tag. These inconsistencies are all handled by the base form classes.
Short form attributes, such as 'required', can be specified with a value of
null
to render properly.There is also a special set of attributes known as controls, which affect rendering or operation, but never render directly as attributes. These all begin with a ':'. One example is the ':showdelete' control in the ButtonTray element which specifies if a 'Delete' button should be included. Each class implementing a control documents it in the __construct DocBlock.
Xoops\Form\Element
extendsXoops\Html\Attributes
which extends\ArrayObject
, so all element attributes can be accessed array style if needed, so there is no need for custom getters and setters, which means "no limits!"There is also a new
ElementFactory
that can create any element using the enhanced specification. See codex/form-element-factory.php for examples.In addition, there are a few breaking changes.
Also, the
Element::setExtra()
method has been deprecated, and will be removed in the future (along with getExtra().) The most common use for this is to add javascript event handlers into the form element. These are much more cleanly implemented using the enhanced specification. For example, this:$button_2->setExtra("onclick='javascript:window.close();'");
becomes this:
$button_2->set('onclick', 'javascript:window.close();');
which is less convoluted, more comprehensible, and multiple event handlers can be easily specified individually, rather than in a big escape quoted blob.