zopefoundation / z3c.form

An advanced form and widget framework for Zope 3
Other
8 stars 39 forks source link

HTMLFormElement.attributes: Allow to extend HTML attributes programmatically. #116

Closed thet closed 1 year ago

thet commented 1 year ago

Came out of necessity and the discussion at: https://github.com/plone/plone.app.z3cform/pull/176

See the z3c.form.browser.widgets.rst doctest on how to use it. Basically it allows for attribute expansion in Chameleon templates like:

<input type="text" tal:attributes="view/attributes" />

You can just set individual attributes like so:

if widget.id == "cancel":
    widget.attributes["formnovalidate"] = "formnovalidate"

There is no need to include the outdated and incomplete list of HTML attributes in the templates anymore. You can just set them via Python.

Note: This attribute expansion does only work that way in Chameleon. Therefore we cannot change any of the templates in z3c.form because - I think - it needs to stay compatible with the Zope PageTemplates reference implementation.

thet commented 1 year ago

@gforcada something like this‌? https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes But I would not include any attribute validation. It's up to the developer to add correct attributes. That allows also for any data-* attributes or special non-standard JS frontend framework attributes.

thet commented 1 year ago

@icemac tnx for the review! I implemented your suggestions.