File

assets/angular/shared/form/validators.ts

Description

Custom RB-Specific Validators - static methods only

Author: Shilo Banihit

Index

Methods

Methods

Static isEmpty
isEmpty(control: AbstractControl)

Forces validation on empty / null values

Parameters :
Name Type Optional Description
control AbstractControl
Returns : literal type
Static noEmptyInGroup
noEmptyInGroup(field: any, dependentFieldNames: string[])

Makes sure all fields have values otherwise this will return an error object containing the emptyFields

Parameters :
Name Type Optional Description
field any
dependentFieldNames string[]
Returns : ValidatorFn
import { ValidatorFn, Validators, AbstractControl, FormGroup } from '@angular/forms';
import * as _ from "lodash-lib";

/**
 * Custom RB-Specific Validators - static methods only
 *
 * Author: <a href='https://github.com/shilob' target='_blank'>Shilo Banihit</a>
 *
 */
export class RbValidator {

  /**
  * Forces validation on empty / null values
  */
  static isEmpty(control: AbstractControl ): {[key: string]: any} {
    return (control && (_.isEmpty(control.value) || control.value.length == 0));
  }

  /**
  *
  * Makes sure all fields have values otherwise this will return an error object containing the emptyFields
  *
  */
  static noEmptyInGroup(field: any, dependentFieldNames: string[]): ValidatorFn {
    return (control: AbstractControl ): {[key: string]: any} => {
      const group = field.formModel;
      if (group) {
        const status: {empty: boolean, emptyFields: any[]} = {empty: false, emptyFields: []};
        _.forEach(dependentFieldNames, (f:any)=> {
          const isEmpty = RbValidator.isEmpty(group.controls[f]);
          if (isEmpty) {
            status.emptyFields.push(f);
          }
          status.empty = status.empty || (isEmpty != null);
        });
        const retval = status.empty ? status : null;
        return retval;
      }
      console.log(`Group doesn't exist yet: ${field.name}`);
    };
  }
}

results matching ""

    No results matching ""