Vanilla Breeze v0.7.4

Vanilla Breeze v0.7.4 released

0.7.4 gives the Ambient pack weather. A page names one feed URL on its own site, and the pack sets data-weather from it, uses its sunrise and sunset for dawn and dusk, and never shows a stale observation. Helpers for producing the feed from Open-Meteo are included. Ambient p…

Read the release notes

formaction

Per-button submission overrides: formaction, formmethod, formenctype, formtarget and formnovalidate let one form send to different URLs, in different ways, depending on the button pressed.

Overview

A submit button can override how its form is submitted. formaction replaces the form's action URL, and four siblings replace the other submission settings for that button only. One form can then have Publish, Save draft and Preview buttons that go to different endpoints, with no script.

Applies to: submit buttons: <button> (type submit, the default), <input type="submit"> and <input type="image">. The button may sit outside the form and point at it with form="id".

Values

Button attributeOverridesValuesWhen absent
formactionactionA URL, resolved against the documentThe form's action; a form without one submits to the current page
formmethodmethodget, post, dialogThe form's method (default get)
formenctypeenctypeapplication/x-www-form-urlencoded, multipart/form-data, text/plainThe form's encoding (default URL-encoded)
formtargettarget_self, _blank, _parent, _top, or a window or iframe nameThe form's target (default _self)
formnovalidatenovalidateBooleanThe form's setting (validate unless novalidate)

Invalid formmethod or formenctype values fall back to get and URL-encoded, not to the form's setting. The button's own name and value are submitted along with the fields, which is another way to tell the server which action was chosen.

Examples

One endpoint, an intent field

If the server prefers one URL, send the choice as the button's value instead.

Closing a dialog

formmethod="dialog" on a button in a form inside a <dialog> closes the dialog instead of sending a request, and sets dialog.returnValue to the button's value.

In Vanilla Breeze

  • data-validate, native mode (the default). After its checks pass, the form coordinator resubmits with form.requestSubmit(submitter), so every override on the pressed button applies: action, method, encoding, target, and its name/value (src/lib/form-coordinator.js).
  • data-submit="fetch" uses the button's formaction, formmethod (get sends a query string; anything else is a POST) and formenctype, and includes its name/value. The POST body follows formenctype, else the form's enctype attribute; with neither authored it is multipart/form-data (not the native urlencoded default), so file inputs work. It ignores formtarget: the response is handled in the page, so there is no window to target. Use a native-mode form for a "Preview in new tab" button.
  • data-submit="event" passes new FormData(form, submitter) in the vb:submit event, so the button's name/value is in the data. The other overrides are up to your handler.
  • formnovalidate in a data-validate form skips the coordinator's checks in every submit mode, so a Save-draft button gets past empty required fields.
  • Wizard. A data-wizard form checks formnovalidate on the clicked submit button and skips its jump to the step with the first error (src/lib/wizard.js).
  • data-loading shows the spinner on the button that actually submitted, using the event's submitter.

Accessibility

  • Each button's text must say what it does ("Save draft", "Preview in new tab"). The overrides are invisible.
  • Warn when a button opens a new window or tab (formtarget="_blank"), in its text or with an icon that has a text alternative.
  • The first submit button in the form is the default for Enter. Put the primary action first in the DOM, or Enter may trigger "Save draft".

Security

A formaction on an injected button sends the whole form, including passwords and CSRF tokens, to another site. Never allow these attributes in user-supplied HTML. VB's HTML sanitizer, used by <markdown-viewer>, comments and other user-content components, strips form, formaction, formmethod, formtarget, formenctype and formnovalidate, and turns submit buttons into plain buttons (src/lib/sanitize-html.js). A Content-Security-Policy with form-action also limits where forms may submit.

Gotchas

  • button.formAction returns the document URL when the attribute is absent, not the form's action. Check button.hasAttribute('formaction') first.
  • form.submit() ignores every button override. Use form.requestSubmit(button).
  • The overrides do nothing on type="button" or type="reset" buttons.
  • formenctype="multipart/form-data" is needed for file uploads with post; with get, only file names are sent.

Related

Which to choose

  • formaction — each action has its own endpoint.
  • name/value on the buttons — one endpoint decides by the submitted intent.
  • formmethod="dialog" — a button that should close a dialog without sending anything.
  • data-submit="fetch" — submit without leaving the page; it keeps formaction, formmethod and formenctype.
↑