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.
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 })
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.
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:
/path
targeting .foo, .baz
. The other validations are queued./path2
targeting .bar, .qux
.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:
up.form.config.validateBatch = true
.[up-validate-batch]
attribute on the element with [up-validate]
.up.validate(element, { batch: false })
.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.
The field or fragment that should be re-rendered on the server.
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.
Whether, when a field is given as element
,
the field's closest form group should be targeted.
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.
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.
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]
.
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.
The URL to which to submit the validation request.
By default Unpoly will use the form's [action]
attribute
The method to use for submitting the validation request.
By default Unpoly will use the form's [method]
attribute
Whether to consolidate multiple validations into a single request.
Defaults to up.form.config.validateBatch
, which defaults to true
.
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.
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.
Disables form controls while the request is loading.
The values of disabled fields will still be included in the submitted form params.
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.
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.
A promise that fulfills when the server-side validation is received and the form was updated.