RAS Playground 日本語 GitHub

User forms

Input validation (validation)

Validates user form input with zod-style rules and shows errors in the label under each control. A control is validated when its input is committed (focus leaves it), and once a control has been committed it is validated again on every later change (the error disappears as soon as you fix the input).

If you put a FormValidator in a module variable of the form's code-behind, the compiler connects the Exit (input commit) and Change events of the form's input controls to its commit / edit.

// forms/frmOrder.ras
import * as z from "validation";

const form = new z.FormValidator();       // placed in a module variable, so the controls' events are connected

function constructor() {
    form.rule(this.txtName, z.string().required("Enter a name").max(20));
}

@on("btnOK.Click")
function ok() {
    if (!form.validateAll()) return;      // validates everything, shows the errors and moves focus to the first error
    this.close(this.txtName.text);
}

Build a schema (ValidationSchema) with z.string() / z.int() / z.number() / z.date() / z.boolean() and chain rule methods onto it (they all return the same schema). The message you pass to int / number / date becomes the error for input of the wrong type ("abc"). When you want to change the rules depending on earlier input, you can call form.rule / form.remove again at any time.

The same schema can also be passed to rule() of a dialog item.

Reference

Class validation.ValidationSchema

Validation rules for an input value (zod-style). Create one with z.string() / z.int() and so on, and chain rules like required() and max(20) (every method returns the same schema).

The value validated is the control's text (text for TextBox / ComboBox / ListBox; for CheckBox and the like, "true" when checked). When the field is blank (including only whitespace), no rule other than required() is checked (a field that is not required may be left empty).

#
type: string = "string"

"string" / "int" / "number" / "date" / "boolean"。

#
typeMessage: string = ""

The message shown when the type check (an integer for z.int() and so on) fails. The default message when empty.

#
required(message: string = ""): ValidationSchema

Makes the input required (for boolean, the box must be checked).

#
optional(): ValidationSchema

Makes the input optional (undoes required()).

#
noTrim(): ValidationSchema

Validates without removing surrounding whitespace (by default it is removed before validation).

#
min(value: double, message: string = ""): ValidationSchema

Minimum (the number of characters for string, the value for int / number, the date's serial value for date).

#
max(value: double, message: string = ""): ValidationSchema

Maximum (the number of characters for string, the value for int / number, the date's serial value for date).

#
length(n: int, message: string = ""): ValidationSchema

The number of characters is exactly n.

#
positive(message: string = ""): ValidationSchema

Greater than 0.

#
nonnegative(message: string = ""): ValidationSchema

0 or greater.

#
regex(pattern: string, message: string = ""): ValidationSchema

Matches the regular expression (VBScript.RegExp syntax).

#
email(message: string = ""): ValidationSchema

The format of an email address.

#
oneOf(values: string[], message: string = ""): ValidationSchema

The value is one of values.

#
refine(fn: (value: string) => boolean, message: string): ValidationSchema

A custom check. When fn returns false, message becomes the error. The values of other controls can be used.

#
check(value: string): string

Validates the value and returns the error message ("" if there is no error).

validation.ras

Function validation.string

function string(message: string = ""): ValidationSchema

A schema for string values.

validation.ras

Function validation.int

function int(message: string = ""): ValidationSchema

A schema for integer values. Input that is not an integer is the error message (when omitted, "Enter an integer").

validation.ras

Function validation.number

function number(message: string = ""): ValidationSchema

A schema for numbers. Input that is not a number is the error message.

validation.ras

Function validation.date

function date(message: string = ""): ValidationSchema

A schema for dates. Input that is not a date is the error message. min / max accept dates (dateSerial(…) and so on).

validation.ras

Function validation.boolean

function boolean(): ValidationSchema

A schema for on / off controls such as check boxes. required() makes checking the box required.

validation.ras

Class validation.FormValidator

Validation of a form's input. Put one in a module variable of the form's code-behind (const form = new FormValidator();), and controls with rules are validated when their input is committed (Exit), with errors shown in a label below the control. Once a control has been committed, it is validated again on every change (Change), so the error disappears as soon as it is fixed.

The label that shows the error is <control name>Error in the same container as the control (txtNameError for txtName); if there is none, one is created right below the control (with a transparent background).

#
errorColor: int = 0x0000CC

The text color of errors.

#
rule(control: MSForms.Control, schema: ValidationSchema, errorLabel: MSForms.Label = null): void

Sets the rules for a control (replacing existing ones). To change the rules based on earlier input, call it again in the same way.

Pass a label of your own as errorLabel to show the error there (its position, color and font are kept: form.rule(this.txtTel, z.string(), errorLabel: this.lblTelMessage)). When omitted, the label <control name>Error is used, or created below the control if there is none. When omitted on a later call, the label used so far is kept. When a different label is passed, the error in the previous label is cleared and shown again in the new one.

#
remove(control: MSForms.Control): void

Removes the control's rules and clears the error shown.

#
has(control: MSForms.Control): boolean

Whether the control has rules.

#
validate(control: MSForms.Control): boolean

Validates the control, shows the error and returns true if there is no error.

#
validateAll(focus: boolean = true): boolean

Validates every control with rules, shows the errors and returns true if all are valid (call it from the submit button). When focus is true, the focus moves to the first control with an error (in the order the rules were set). Hidden and disabled controls are not validated.

#
isValid(): boolean

Checks whether everything is valid without showing errors.

#
error(control: MSForms.Control): string

The error shown ("" if none).

#
setError(control: MSForms.Control, message: string): void

Shows an error directly ("" clears it). Use it for the result of checks that rules cannot express, such as a server's response. It can also be shown for controls without rules.

#
reset(): void

Clears every error and returns to the uncommitted state (the rules are kept).

#
commit(control: MSForms.Control): void

Commits the input (the control's Exit). Called from the event procedures the compiler generates.

#
edit(control: MSForms.Control): void

A change of the input (the control's Change). Validates again the controls already committed and the controls whose changes are commits at once (CheckBox, OptionButton, ToggleButton, ListBox). Called from the event procedures the compiler generates.

#
commitActive(container: MSForms.Control): void

When the focus leaves a container (Frame / MultiPage), commits the control inside that had the focus (MSForms does not raise Exit for a control inside a frame when the focus moves out of the frame). Called from the event procedures the compiler generates.

validation.ras