ValidationErrors

Useful helper class to work with validation errors.

Constructor

Returns an instance of the ValidationErrors class.

Example:

  import {ValidationErrors} from 'uikernel';

  const validationErrors = new ValidationErrors();

Methods


(static) createFromJSON

  const validationErrors = ValidationErrors.createFromJSON(jsonObject);

Convert object with a description of errors into ValidationErrors instance.

Parameters:

Type Name Description
Object jsonObject 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 validationErrors = ValidationErrors.createFromJSON({
    login: "invalid login!",
    pass: "invalid password!"
  });

(static) createWithError

Create ValidationErrors instance with one error.

Parameters:

Type Name Description
string field Field name
string error Error text

Returns: new ValidationErrors instance.

Example:

  const validationErrors = ValidationErrors.createWithError('message', 'Massage is required');

(static) merge

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

Unites several ValidationErrors instances into one.

Parameters:

Type Name Description
ValidationErrors error1 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

  validationErrors.add(field, errorText);

Add en error into the ValidationErrors instance.

Parameters:

Type Name Description
string field Field name
string errorText Error text

Returns: this

Example:

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

clear

  validationErrors.clear();

Clear errors list.

Returns: this


clearField

  validationErrors.clearField(field);

Clear errors of the specified field.

Parameters:

Type Name Description
string field Name of the field to be cleared from errors

Returns: this

Example:

  validationErrors.clearField("email");

clone

  const validationErrors2 = validationErrors.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 = validationErrors.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 validationErrors.getErrors())
    console.log(field, ": ", error);

getFailedFields

  const fieldsWithErrors = validationErrors.getFailedFields();

Get field names array, that contain errors.

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

Example:

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

getFieldErrorMessages

  const loginErrors = validationErrors.getFieldErrorMessages(field);

Get errors messages of the specified field.

Parameters:

Type Name Description
string field Field name to get errors of

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

Example:

  const loginErrors = validationErrors.getFieldErrorMessages("login");   //["invalid login!"]

hasError

  const loginIsValid = validationErrors.hasError(field);

Check if the field has any errors.

Parameters:

Type Name Description
string field Field name to check validity of

Returns: Boolean

Example:

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

isEmpty

  const formIsValid = validationErrors.isEmpty(field);

Check if this ValidationErrors instance has any field with errors.

Returns: Boolean

Example:

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

toJSON

  const formErrorsObj = validationErrors.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 = validationErrors.toJSON();
  // formErrorsObj === {
  //   login: ["invalid login!"],
  //   pass: ["invalid password!"],
  //   phone: ["The phone number field is invalid!"]
  // }