@serenity-is/corelib / ValidatorOptions

Interface: ValidatorOptions

Table of contents

Properties

Methods

Properties

debug

Optional debug: boolean

True for logging debug info

Defined in

src/base/validator.ts:60


errorClass

Optional errorClass: string

Use this class to create error labels, to look for existing error labels and to add it to invalid elements.

default: "error"

Defined in

src/base/validator.ts:67


errorElement

Optional errorElement: string

Use this element type to create error messages and to look for existing error messages. The default, "label", has the advantage of creating a meaningful link between error message and invalid field using the for attribute (which is always used, regardless of element type).

default: "label"

Defined in

src/base/validator.ts:75


focusInvalid

Optional focusInvalid: boolean

Focus the last active or first invalid element on submit via validator.focusInvalid(). The last active element is the one that had focus when the form was submitted, avoiding stealing its focus. If there was no element focused, the first one in the form gets it, unless this option is turned off.

default: true

Defined in

src/base/validator.ts:91


ignore

Optional ignore: string

Elements to ignore when validating, simply filtering them out. CSS not-method is used, therefore everything that is accepted by not() can be passed as this option. Inputs of type submit and reset are always ignored, so are disabled elements.

Defined in

src/base/validator.ts:104


messages

Optional messages: Record<string, string>

Key/value pairs defining custom messages. Key is the name of an element, value the message to display for that element. Instead of a plain message, another map with specific messages for each rule can be used. Overrides the title attribute of an element or the default message for the method (in that order). Each message can be a String or a Callback. The callback is called in the scope of the validator, with the rule's parameters as the first argument and the element as the second, and must return a String to display as the message.

default: the default message for the method used

Defined in

src/base/validator.ts:120


normalizer

Optional normalizer: (val: ValidationValue, element: ValidatableElement) => string

Type declaration

▸ (val, element): string

Parameters
Name Type
val ValidationValue
element ValidatableElement
Returns

string

Defined in

src/base/validator.ts:122


onclick

Optional onclick: boolean | ValidateEventDelegate

Boolean or Function. Validate checkboxes and radio buttons on click. Set to false to disable.

Set to a Function to decide for yourself when to run validation. A boolean true is not a valid value.

Defined in

src/base/validator.ts:130


onfocusin

Optional onfocusin: ValidateEventDelegate

Function. Validate elements when user focuses in. If omitted hides all other fields marked as invalid.

Set to a custom Function to decide for yourself when to run validation.

Defined in

src/base/validator.ts:137


onfocusout

Optional onfocusout: ValidateEventDelegate

Boolean or Function. Validate elements (except checkboxes/radio buttons) on blur. If nothing is entered, all rules are skipped, except when the field was already marked as invalid.

Set to a Function to decide for yourself when to run validation. A boolean true is not a valid value.

Defined in

src/base/validator.ts:145


onkeyup

Optional onkeyup: ValidateEventDelegate

Boolean or Function. Validate elements on keyup. As long as the field is not marked as invalid, nothing happens. Otherwise, all rules are checked on each key up event. Set to false to disable.

Set to a Function to decide for yourself when to run validation. A boolean true is not a valid value.

Defined in

src/base/validator.ts:154


onsubmit

Optional onsubmit: boolean

Validate the form on submit. Set to false to use only other events for validation. Set to a Function to decide for yourself when to run validation. A boolean true is not a valid value.

default: true

Defined in

src/base/validator.ts:163


pendingClass

Optional pendingClass: string

Pending class default: "pending"

Defined in

src/base/validator.ts:169


rules

Optional rules: ValidationRulesMap

A custom message display handler. Gets the map of errors as the first argument and an array of errors as the second, called in the context of the validator object. The arguments contain only those elements currently validated, which can be a single element when doing validation onblur/keyup. You can trigger (in addition to your own messages) the default behaviour by calling this.defaultShowErrors().

Defined in

src/base/validator.ts:177


success

Optional success: string | (label: HTMLElement, validatedInput: ValidatableElement) => void

String or Function. If specified, the error label is displayed to show a valid element. If a String is given, it is added as a class to the label. If a Function is given, it is called with the label and the validated input (as a DOM element). The label can be used to add a text like "ok!".

Defined in

src/base/validator.ts:200


validClass

Optional validClass: string

This class is added to an element after it was validated and considered valid.

default: "valid"

Defined in

src/base/validator.ts:214

Methods

abortHandler

abortHandler(validator): void

Parameters

Name Type
validator Validator

Returns

void

Defined in

src/base/validator.ts:187


errorPlacement

errorPlacement(error, element, validator): void

Customize placement of created error labels. First argument: The created error label. Second argument: The invalid element.

default: Places the error label after the invalid element

Parameters

Name Type
error HTMLElement
element ValidatableElement
validator Validator

Returns

void

Defined in

src/base/validator.ts:82


highlight

highlight(element, errorClass, validClass): void

How to highlight invalid fields. Override to decide which fields and how to highlight.

default: Adds errorClass (see the option) to the element

Parameters

Name Type
element ValidatableElement
errorClass string
validClass string

Returns

void

Defined in

src/base/validator.ts:98


invalidHandler

invalidHandler(event, validator): void

Callback for custom code when an invalid form is submitted. Called with an event object as the first argument, and the validator as in the second.

Parameters

Name Type
event Event
validator Validator

Returns

void

Defined in

src/base/validator.ts:110


showErrors

showErrors(errorMap, errorList, validator): void

A custom message display handler. Gets the map of errors as the first argument and an array of errors as the second, called in the context of the validator object. The arguments contain only those elements currently validated, which can be a single element when doing validation onblur/keyup. You can trigger (in addition to your own messages) the default behaviour by calling this.defaultShowErrors().

Parameters

Name Type
errorMap ValidationErrorMap
errorList ValidationErrorList
validator Validator

Returns

void

Defined in

src/base/validator.ts:185


submitHandler

submitHandler(form, event, validator): boolean | void

Callback for handling the actual submit when the form is valid. Gets the form and the event object. Replaces the default submit. The right place to submit a form via Ajax after it is validated.

Parameters

Name Type
form HTMLFormElement
event Event
validator Validator

Returns

boolean | void

Defined in

src/base/validator.ts:193


unhighlight

unhighlight(element, errorClass, validClass, validator): void

Called to revert changes made by option highlight, same arguments as highlight.

default: Removes the errorClass

Parameters

Name Type
element ValidatableElement
errorClass string
validClass string
validator Validator

Returns

void

Defined in

src/base/validator.ts:207