File

src/lib/services/base-validator-service/web-forms-base-validator.service.ts

Extends

BaseValidatorService

Index

Properties
Methods

Constructor

constructor()

Base constructor of the service

Methods

addCompareValidator
addCompareValidator(elementToCompare: string, validator: BaseValidatorComponent)

Adds a compate validator to the service

Parameters :
Name Type Optional
elementToCompare string No
validator BaseValidatorComponent No
Returns : void
addValidationSummary
addValidationSummary(summary: ValidationSummaryComponent)

Adds a validation summary to the service

Parameters :
Name Type Optional
summary ValidationSummaryComponent No
Returns : void
addValidator
addValidator(validator: BaseValidatorComponent)

Adds a validator to the service

Parameters :
Name Type Optional
validator BaseValidatorComponent No
Returns : void
checkValidators
checkValidators(validationGroup: string)

Checks the state of the validators to see if the inputs are valid. If the validation failed, show them.

Parameters :
Name Type Optional
validationGroup string No
Returns : boolean

if the page validations are right

getActiveErrorMessages
getActiveErrorMessages(validationGroup: string)

Retrieve the error messages of the validators with a given validationGroup.

Parameters :
Name Type Optional Description
validationGroup string No

The validation group of the validators.

Returns : string[]

An array of strings with the error messages.

hideValidationSumaries
hideValidationSumaries(validationGroup: string)

Hide the validation summaries with the given validation group

Parameters :
Name Type Optional
validationGroup string No
Returns : void
removeValidator
removeValidator(validator: BaseValidatorComponent)

Removes a validator from the service.

Parameters :
Name Type Optional
validator BaseValidatorComponent No
Returns : void
showValidationSumaries
showValidationSumaries(validationGroup: string)

Shows the validation summaries with the given validation group

Parameters :
Name Type Optional
validationGroup string No
Returns : void
validate
validate(controlToValidate: string, expressionToValidate: string)

Calls to the validation method of the validators attached to a control.

Parameters :
Name Type Optional
controlToValidate string No
expressionToValidate string No
Returns : boolean

if none of the validations failed.

Properties

Private validationSummaries
Type : ValidationSummaryComponent[]
Default value : []

Property with the validators registered.

Private validators
Default value : new Map<string, BaseValidatorComponent[]>()

Property with the validators registered.

import { Injectable } from '@angular/core';
import { BaseValidatorService } from '@mobilize/winforms-components';
import { ValidationSummaryComponent } from '../../components/validation-summary/validation-summary.component';
import { BaseValidatorComponent } from '../../components/base-validator/BaseValidator';
import { ValidatorDisplay } from '../../contracts/ValidatorDisplay';

@Injectable({
  providedIn: 'root'
})
export class WebFormsBaseValidatorService extends BaseValidatorService {
  /**
   * Property with the validators registered.
   *
   * @memberof WebFormsBaseValidatorService
   */
  private validators = new Map<string, BaseValidatorComponent[]>();

  /**
   * Property with the validators registered.
   *
   * @memberof WebFormsBaseValidatorService
   */
  private validationSummaries: ValidationSummaryComponent[] = [];

  /**
   * Base constructor of the service
   *
   * @memberof WebFormsBaseValidatorService
   */
  constructor() {
    super();
  }

  /**
   * Removes a validator from the service.
   *
   * @param controlToValidate
   * @memberof WebFormsBaseValidatorService
   */
  removeValidator(validator: BaseValidatorComponent): void {
    const validatorArray = this.validators.get(validator.controlToValidate);
    if (validatorArray != undefined) {
      const validatorIndex = validatorArray.indexOf(validator);
      if (validatorIndex > -1) {
        validatorArray.splice(validatorIndex, 1);
        if (validatorArray.length === 0) {
          this.validators.delete(validator.controlToValidate);
        }
      }
    }
  }

  /**
   * Adds a validator to the service
   *
   * @param validator
   * @memberof WebFormsBaseValidatorService
   */
  addValidator(validator: BaseValidatorComponent): void {
    const validatorArray = this.validators.get(validator.controlToValidate);
    if (validatorArray != undefined) {
      validatorArray.push(validator);
    } else {
      this.validators.set(validator.controlToValidate, [validator]);
    }
  }

  /**
   * Adds a validation summary to the service
   * @param summary
   * @memberof WebFormsBaseValidatorService
   */
  addValidationSummary(summary: ValidationSummaryComponent) {
    this.validationSummaries.push(summary);
  }

  /**
   * Adds a compate validator to the service
   *
   * @param elementToCompare
   * @param validator
   * @memberof WebFormsBaseValidatorService
   */
  addCompareValidator(
    elementToCompare: string,
    validator: BaseValidatorComponent
  ): void {
    const validatedArray = this.validators.get(validator.controlToValidate);
    const comparatedArray = this.validators.get(elementToCompare);
    if (validatedArray != undefined) {
      validatedArray.push(validator);
    } else {
      this.validators.set(validator.controlToValidate, [validator]);
    }
    if (comparatedArray != undefined) {
      comparatedArray.push(validator);
    } else {
      this.validators.set(elementToCompare, [validator]);
    }
  }

  /**
   * Calls to the validation method of the validators attached to a control.
   *
   * @param controlToValidate
   * @param expressionToValidate
   * @memberof WebFormsBaseValidatorService
   * @returns if none of the validations failed.
   */
  validate(controlToValidate: string, expressionToValidate: string): boolean {
    let passed = true;
    const validatorArray = this.validators.get(controlToValidate);
    if (validatorArray != undefined) {
      validatorArray.forEach((validator) => {
        if (
          !validator.validateControl(controlToValidate, expressionToValidate) &&
          passed
        ) {
          passed = false;
        }
      });
    }
    return passed;
  }

  /**
   * Checks the state of the validators to see if the inputs are valid.
   * If the validation failed, show them.
   * @memberof WebFormsBaseValidatorService
   * @returns if the page validations are right
   */
  checkValidators(validationGroup: string): boolean {
    let valid = true;
    this.validators.forEach((value) => {
      value.forEach((validator) => {
        const element = document.querySelector(`#${validator.name}`);
        if (
          element &&
          validator.validationGroup === validationGroup &&
          !validator.validationSuccess
        ) {
          if (validator.Display !== ValidatorDisplay.None) {
            validator.hidden = false;
          } else {
            validator.hidden = true;
          }

          valid = false;
        }
      });
    });
    !valid
      ? this.showValidationSumaries(validationGroup)
      : this.hideValidationSumaries(validationGroup);

    return valid;
  }

  /**
   * Shows the validation summaries with the given validation group
   *
   * @param validationGroup
   * @memberof WebFormsBaseValidatorService
   */
  showValidationSumaries(validationGroup: string): void {
    this.validationSummaries.forEach((summary) => {
      if (summary.validationGroup === validationGroup) {
        summary.updateErrorMessages();
        summary.visible = true;
      }
    });
  }

  /**
   * Hide the validation summaries with the given validation group
   *
   * @param validationGroup
   * @memberof WebFormsBaseValidatorService
   */
  hideValidationSumaries(validationGroup: string): void {
    this.validationSummaries.forEach((summary) => {
      if (summary.validationGroup === validationGroup) {
        summary.visible = false;
      }
    });
  }

  /**
   * Retrieve the error messages of the validators with a given validationGroup.
   *
   * @param validationGroup The validation group of the validators.
   * @memberof WebFormsBaseValidatorService
   * @returns An array of strings with the error messages.
   */
  getActiveErrorMessages(validationGroup: string): string[] {
    const errorMessages: string[] = [];
    const currentValidators = new Set();
    this.validators.forEach((value) => {
      value.forEach((validator) => {
        const element = document.querySelector(`#${validator.name}`);
        if (
          element &&
          !validator.validationSuccess &&
          validator.validationGroup === validationGroup
        ) {
          if (!currentValidators.has(validator.id)) {
            errorMessages.push(validator.errorMessage);
            currentValidators.add(validator.id);
          }
        }
      });
    });
    return errorMessages;
  }
}

results matching ""

    No results matching ""