ValidationErrors

Useful helper class to work with validation errors.

Constructor

Returns an instance of the ValidationErrors class.

Example:

  const myErrorsInstance1 = new ValidationErrors();

Methods


(static) createFromJSON

  const myErrorsInstance2 = ValidationErrors.createFromJSON(jsonObject);

Convert object with a description of errors into ValidationErrors instance.

Parameters:

Type Name Description
Object jsonObject Required. An ValidationErrors description Object that will be turned into its instance. Expected structure:
    {field1: [“field1 error1”], field2: [“field2 error1”, “field2 error2”], …}

Returns: new ValidationErrors instance.

Example:

  const myErrorsInstance2 = ValidationErrors.createFromJSON({
    login: "invalid login!",
    pass: "invalid password!"
  });

(static) merge

  const myErrorsInstance3 = ValidationErrors.merge(error1, error2, ..., errorN);

Unites several ValidationErrors instances into one.

Parameters:

Type Name Description
ValidationErrors error1 Required. A ValidationErrors instance to be united with others passed in arguments
ValidationErrors error2 Optional. A ValidationErrors instance to be united with others passed in arguments
ValidationErrors errorN Optional. A ValidationErrors instance to be united with others passed in arguments

Returns: new ValidationErrors instance.


add

  myErrorsInstance3.add(field, errorText);

Add en error into the ValidationErrors instance.

Parameters:

Type Name Description
String field Required. Field name
String errorText Required. Error text

Returns: this

Example:

  myErrorsInstance3
    .add("email", "The email field is invalid!")
    .add("phone", "The phone number field is invalid!");

clear

  myErrorsInstance1.clear();

Clear errors list.

Returns: this


clearField

  myErrorsInstance3.clearField(field);

Clear errors of the specified field.

Parameters:

Type Name Description
String field Required. Name of the field to be cleared from errors

Returns: this

Example:

  myErrorsInstance3.clearField("email");

clone

  const myErrorsInstance3copy = myErrorsInstance3.clone();

Return a new instance cloning this, so that the new instance will have the same structure, but a new memory address.

Returns: this


getErrors

  const myErrors = myErrorsInstance.getErrors();

Get errors entries of this ValidationErrors instance.

Returns: Array with the following structure: [[“field1”, [“field1 error1”]], [“field2”, [“field2 error1”, “field2 error2”]], …]

Example:

  for(let [field, error] of myErrorsInstance3.getErrors())
    console.log(field, ": ", error);

getFailedFields

  const fieldsWithErrors = myErrorsInstance.getFailedFields();

Get field names array, that contain errors.

Returns: String[] or null(if there is no errors)

Example:

  const fieldsWithErrors3 = myErrorsInstance3.getFailedFields();   //["login", "pass", "phone"]
  const fieldsWithErrors7 = myErrorsInstance7.getFailedFields();   //null

getFieldErrors

  const loginErrors = myErrorsInstance3.getFieldErrors(field);

Get errors of the specified field.

Parameters:

Type Name Description
String field Required. Field name to get errors of

Returns: String[] or null(if there is no errors)

Example:

  const loginErrors = myErrorsInstance3.getFieldErrors("login");   //["invalid login!"]

hasError

  const loginIsValid = myErrorsInstance.hasError(field);

Check if the field has any errors.

Parameters:

Type Name Description
String field Required. Field name to check validity of

Returns: Boolean

Example:

  const loginIsValid = myErrorsInstance3.hasError("login");   //false

isEmpty

  const formIsValid = myErrorsInstance1.isEmpty(field);

Check if this ValidationErrors instance has any field with errors.

Returns: Boolean

Example:

  const formIsValid = myErrorsInstance1.isEmpty();   //true

toJSON

  const formErrorsObj = myErrorsInstance3.toJSON();

Convert this ValidationErrors instance to plain JS Object.

Returns: Object with the following structure: {field1: [“field1 error1”], field2: [“field2 error1”, “field2 error2”], …}

Example:

  const formErrorsObj = myErrorsInstance3.toJSON();
  //formErrorsObj === {login: ["invalid login!"], pass: ["invalid password!"], phone: ["The phone number field is invalid!"]}