Edit this page

up.form up.validate(element, options, [options])
JavaScript function

Render a new form state from its current field values, to show validation errors or update dependent elements.

Typical use cases are to show validation errors after a field was changed or to update forms where one field depends on the value of another.

up.validate() submits the given element's form with an additional X-Up-Validate HTTP header. Upon seeing this header, the server is expected to validate (but not commit) the form submission and render a new form state. See this example for control flow on the server.

To automatically update a form after a field was changed, use the the [up-validate] attribute. You may combine [up-validate] and up.validate() within the same form. In order to reduce requests, their updates will be batched together.

Controlling what is updated

up.validate() always submits the entire form with its current field values to the form's [action] path. Typically only a fragment of the form is updated with the response. This minimizes the chance for loss of transient state like scroll positions, cursor selection or user input while the request is in flight.

Passing a form field will update the closest form group around the field:

up.validate('input[name=email]')

If the given field has an [up-validate] attribute with a custom target selector, that selector will be updated instead.

You may also update arbitrary elements within the form:

up.validate('.preview')

You may also choose to re-render an entire form. In this case it is recommended to disable fields while rendering. This prevents the loss of user input made while the request is in flight:

up.validate('form', { disable: true })

Multiple validations are batched together

In order to reduce requests, multiple calls of up.validate() within the same task are consolidated into a single request. For instance, the following will send a single request targeting .foo, .bar:

up.validate('.foo')
up.validate('.bar')

Validating the same target multiple times will also only send a single request. For instance, the following will send a single request targeting .qux:

up.validate('.qux')
up.validate('.qux')

When one of your target elements is an ancestor of another target, Unpoly will only request the ancestor. For instance, the following would send a single request targeting form:

up.validate('input[name=email]')
up.validate('form')

When a validation request is already in flight, additional validations are queued. When the current request has loaded, queued validations are batched using the same rules as outlined above.

Batching with multiple URLs

Even with multiple URLs, only a single validation request per form will be in flight at the same time.

Batches will be partitioned by URL. Batch requests are sent in sequence, with no concurrency. Additional validations are queued until the current batch request has loaded.

For instance, let's assume the following four validations are queued:

up.validate('.foo', { url: '/path1' })
up.validate('.bar', { url: '/path2' })
up.validate('.baz', { url: '/path1' })
up.validate('.qux', { url: '/path2' })

This will send a sequence of two requests:

  1. A request to /path targeting .foo, .baz. The other validations are queued.
  2. Once that request finishes, a second request to /path2 targeting .bar, .qux.

Disabling batching

By disabling batching, Unpoly will send individual requests for each call to up.validate() and for each change of an [up-validate] field. Additional validations are queued until the current validation request has loaded.

There are multiple ways to disable batching:

  • Globally configuring up.form.config.validateBatch = true.
  • Setting an [up-validate-batch] attribute on the element with [up-validate].
  • Passing an option up.validate(element, { batch: false }).

Preventing race conditions

Unpoly guarantees that many concurrent validations will eventually show a consistent form state, regardless of how fast the user clicks or how slow the network is.

See preventing race conditions for details.


Targeting

element
required

The field or fragment that should be re-rendered on the server.

See controlling what is updated.

[options.target=element]
optional

The target selector to re-render.

By default the given element will be rendered. If element is a field, its form group or [up-validate] target will be rendered.

string
[options.formGroup=true]
optional

Whether, when a field is given as element, the field's closest form group should be targeted.

boolean
[options.origin=element]
optional

The element or field that caused this validation pass.

The names of all fields contained within the origin will be passed as an X-Up-Validate request header.

Render options

[options]
optional

Additional render options to use when re-rendering the targeted fragment.

Common options are documented below, but most options for up.submit() may be used.

Note that validation requests may be batched together. In this case Unpoly will try to merge render options where possible (e.g. { headers, target }). When a render option cannot be merged (e.g. { scroll }), the option from the last validation in the batch will be used.

Client state

[options.keepData]
optional

Whether to preserve the fragment's data object throughout the update.

Properties from the new fragment's [up-data] are overridden with the old fragment's [up-data].

boolean
[options.data]
optional

The data object object for the new fragment.

If the new fragment already has an [up-data] object, this option will override individual properties from that attribute.

[options.keep=true]
optional

Whether [up-keep] elements will be preserved in the updated fragments.

boolean
[options.context]
optional

An object that will be merged into the context of the current layer once the fragment is rendered.

Request

options.url
required

The URL to which to submit the validation request.

By default Unpoly will use the form's [action] attribute

See Validating against other URLs.

string
options.method
required

The method to use for submitting the validation request.

By default Unpoly will use the form's [method] attribute

See Validating against other URLs.

string
[options.batch=true]
optional
boolean
[options.params]
optional

Additional Form parameters that should be sent as the request's query string or payload.

The given value will be added to params parsed from the form's input fields. If a param with the same name already existed in the form, it will be deleted and overridden with the given value.

[options.headers={}]
optional

An object with additional request headers.

Unpoly will by default send a number of custom request headers. E.g. the X-Up-Target header includes the targeted CSS selector. See up.protocol for details.

Loading state

[options.disable]
optional

Disables form controls while the request is loading.

The values of disabled fields will still be included in the submitted form params.

booleanstringElementArray
[options.placeholder]
optional

A placeholder to show within the targeted fragment while new content is loading.

Existing children of the targeted fragment will be hidden during the request. When the request ends for any reason, all changes will be reverted.

stringElementList<Node>
[options.preview]
optional

One or more previews that temporarily change the page while new content is loading.

The preview changes will be reverted automatically when the request ends for any reason.

stringFunction(up.Preview)Array
[options.feedback=true]
optional

Whether to set feedback classes while loading content.

boolean

Return value

A promise that fulfills when the server-side validation is received and the form was updated.