sercrod

:formaction

Summary

:formaction is an attribute binding directive that controls the formaction attribute on form submit controls such as button and input type="submit". It evaluates a Sercrod expression and writes the result into the formaction attribute, optionally passing the value through a configurable url filter. The binding is one way: data updates change the DOM attribute, but changes to the DOM attribute do not update data.

:formaction is part of the generic colon attribute family and shares the same evaluation rules as other colon bindings such as :href, :src, and :action.

Basic example

A form with per button endpoints:

<serc-rod id="app" data='{
  "saveEndpoint": "/api/save",
  "deleteEndpoint": "/api/delete"
}'>
  <form method="post" action="/api/default">
    <button type="submit" :formaction="saveEndpoint">
      Save
    </button>
    <button type="submit" :formaction="deleteEndpoint">
      Delete
    </button>
  </form>
</serc-rod>

Behavior:

Behavior

Core rules:

Evaluation timing

:formaction is evaluated during the element rendering phase, together with other colon style attribute bindings.

More precisely:

There is no separate scheduling or delay specific to :formaction. It participates in the normal render update cycle of the host.

Execution model

Conceptually, when Sercrod renders an element with :formaction, the steps are:

  1. Sercrod encounters a node that has an attribute named :formaction.

  2. It reads the expression string from that attribute, for example endpoint or mode === 'archive' ? archiveUrl : saveUrl.

  3. It evaluates the expression with eval_expr, using the current scope and a context that sets mode to attr:formaction and el to the current element.

  4. It inspects the result:

    • If the result is strictly false, or is null or undefined, Sercrod removes the formaction attribute from the element and stops.
    • Otherwise, it converts the result to a string and passes it to the url filter, along with the attribute name "formaction" and a context with the element and scope.
  5. If the url filter returns a truthy string, Sercrod sets the element attribute formaction to that string.

  6. If evaluation throws an exception at any point, Sercrod falls back to setting formaction to an empty string.

  7. If cleanup.handlers is enabled in the configuration, Sercrod removes the original :formaction attribute from the element, leaving only the concrete formaction attribute in the output DOM.

Use with forms and actions

Because :formaction is a pure attribute binding, it composes naturally with the rest of the form and with :action:

Use with structural directives and loops

:formaction is evaluated at the element level and does not interfere with container level structural directives.

Examples:

Best practices

Additional examples

Switching endpoint based on environment:

<serc-rod id="app" data='{
  "env": "staging",
  "endpoints": {
    "staging": "https://staging.example.com/api/bulk",
    "production": "https://example.com/api/bulk"
  }
}'>
  <form method="post" action="/noop">
    <button type="submit" :formaction="endpoints[env]">
      Run bulk job
    </button>
  </form>
</serc-rod>

Here, changing env in data (for example from "staging" to "production") changes the target endpoint for submissions triggered by the button.

Notes