Home Reference Source
import FormConstraint from '~/controllers/Form/FormConstraint.js'
public class | source

FormConstraint

Specifies what constraints a given form field must satisfy

Static Method Summary

Static Public Methods
public static

observeValidation(validationObservers: ...Observable<ValidationError[]>): Observable<boolean>

Takes list of observers over which to observe validation.

Constructor Summary

Public Constructor
public

Creates a form constraint (doing nothing).

Method Summary

Public Methods
public

addValidator(callback: Function, error: string, opts: Object)

Adds a validator to execute

public

Flatten a validator into a single one

public

Checks if has a value

public

Checks if a field is an email

public

Allows the value to either follow validations or be empty.

public

Specifies the length must be between (inclusive) bounds between a min and a max.

public

Makes sure a form value is not empty

public

Adds an OR validator.

public

Requires a field to match a certain regex.

public

validate(value: any): ValidationError[]

Runs validation on the element.

Static Public Methods

public static observeValidation(validationObservers: ...Observable<ValidationError[]>): Observable<boolean> source

Takes list of observers over which to observe validation.

Params:

NameTypeAttributeDescription
validationObservers ...Observable<ValidationError[]>

Return:

Observable<boolean>

emits beginning with false

Public Constructors

public constructor() source

Creates a form constraint (doing nothing).

Public Methods

public addValidator(callback: Function, error: string, opts: Object) source

Adds a validator to execute

Params:

NameTypeAttributeDescription
callback Function

Validator to call. Passed element for first arg.

error string

String to print on error if the validation fails.

opts Object

additional options

public flatten(constraint: FormConstraint): FormConstraint source

Flatten a validator into a single one

Params:

NameTypeAttributeDescription
constraint FormConstraint

Return:

FormConstraint

chainable object

public hasValue(error: string): FormConstraint source

Checks if has a value

Params:

NameTypeAttributeDescription
error string

Error to show

Return:

FormConstraint

chainable object

public isEmail(): FormConstraint source

Checks if a field is an email

Return:

FormConstraint

chainable object.

public isEmpty(): FormConstraint source

Allows the value to either follow validations or be empty. Empty is defined as null or having no length.

Return:

FormConstraint

chainable object.

public length(min: number, max: number): FormConstraint source

Specifies the length must be between (inclusive) bounds between a min and a max. For input elements.

Params:

NameTypeAttributeDescription
min number

a positive integer representing the minimum length.

max number

a positive integer representing the maximum length.

Return:

FormConstraint

chainable object.

public notEmpty(error: string): FormConstraint source

Makes sure a form value is not empty

Params:

NameTypeAttributeDescription
error string

error string to show.

Return:

FormConstraint

chainable object.

public or(caseA: FormConstraint, caseB: FormConstraint): FormConstraint source

Adds an OR validator.

Params:

NameTypeAttributeDescription
caseA FormConstraint

The first case

caseB FormConstraint

The second case

Return:

FormConstraint

chainable object.

public regex(regex: RegExp): FormConstraint source

Requires a field to match a certain regex.

Params:

NameTypeAttributeDescription
regex RegExp

Regex to match elem.value to.

Return:

FormConstraint

chainable object.

public validate(value: any): ValidationError[] source

Runs validation on the element.

Params:

NameTypeAttributeDescription
value any

Any value to run validator on

Return:

ValidationError[]

list of form errors. Empty array if none.