assets/angular/shared/form/validators.ts
Custom RB-Specific Validators - static methods only
Author: Shilo Banihit
Methods |
|
Static isEmpty | ||||||||
isEmpty(control: AbstractControl)
|
||||||||
Defined in assets/angular/shared/form/validators.ts:34
|
||||||||
Forces validation on empty / null values
Parameters :
Returns :
literal type
|
Static noEmptyInGroup | ||||||||||||
noEmptyInGroup(field: any, dependentFieldNames: string[])
|
||||||||||||
Defined in assets/angular/shared/form/validators.ts:43
|
||||||||||||
Makes sure all fields have values otherwise this will return an error object containing the emptyFields
Parameters :
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}`);
};
}
}