up.Params up.Params.fromForm(form, [options])
Class method

Constructs a new up.Params instance from the given <form>.

The returned params may be passed as { params } option to up.request() or up.render().

The params are built with the browser's own form-data algorithm, so they contain exactly what a regular form submission would send:

  • All <input> types are supported.
  • Field values are usually strings, but an <input type="file"> will produce File values.
  • An <input type="radio"> or <input type="checkbox"> is only added if it is checked.
  • A <select> adds every selected option.
  • Fields that are disabled, or inside a <fieldset disabled>, are ignored.
  • Fields without a [name] attribute are ignored.
  • Fields outside the form that reference it with a [form] attribute are included.
  • Form-associated custom elements are included.
  • Values appended by a formdata event listener are included.
  • The submit button contributes its [name] and [value].

On top of that, elements registered as fields in up.form.config.fieldSelectors are included. The browser cannot know about those.

Example

Given this HTML form:

<form id="signup">
  <input type="text" name="email" value="foo@bar.com">
  <input type="password" name="pass" value="secret">
</form>

This serializes the form into params:

let params = up.Params.fromForm(document.querySelector('#signup'))
let email = params.get('email') // email is now 'foo@bar.com'
let pass = params.get('pass') // pass is now 'secret'

Submit buttons

A submit button only contributes params when it is the one that submitted the form. Pass the button as a { submitButton } option, or false to submit no button at all. Without the option the first submit button is assumed, which is also what a browser does when the user submits from a field by pressing Enter.

The button must belong to the form. Like the browser's FormData, up.Params.fromForm() throws an error for a button of another form.

Disabled fields

Like in a browser submission, a disabled field contributes no params. The exceptions are fields and buttons that a preview disabled while a request is loading, e.g. with [up-disable]. These still count as part of the form, so a second submission sends the same params as the first one.


Parameters

form
required

A <form> element.

Type: Element
[options.submitButton]
optional

The submit button that submitted the form.

The button must belong to the form, or an error is thrown. Pass false to include no submit button.

Type: Element | false

Return value

A new up.Params instance with values from the given form.

Type: up.Params