mirror of
https://github.com/bitwarden/jslib
synced 2025-12-15 15:53:51 +00:00
* Extract SsoConfig enums to own file * Add ChangeStripSpaces directive * Move custom validators to jslib * Add a11y-invalid directive * Add and implement dirtyValidators * Create ssoConfigView model and factory methods * Add interface for select options * Don't build SsoConfigData if null Co-authored-by: Oscar Hinton <oscar@oscarhinton.com>
26 lines
1.0 KiB
TypeScript
26 lines
1.0 KiB
TypeScript
import { AbstractControl, ValidationErrors, ValidatorFn, Validators } from "@angular/forms";
|
|
import { requiredIf } from "./requiredIf.validator";
|
|
|
|
/**
|
|
* A higher order function that takes a ValidatorFn and returns a new validator.
|
|
* The new validator only runs the ValidatorFn if the control is dirty. This prevents error messages from being
|
|
* displayed to the user prematurely.
|
|
*/
|
|
function dirtyValidator(validator: ValidatorFn) {
|
|
return (control: AbstractControl): ValidationErrors | null => {
|
|
return control.dirty ? validator(control) : null;
|
|
};
|
|
}
|
|
|
|
export function dirtyRequiredIf(predicate: (predicateCtrl: AbstractControl) => boolean) {
|
|
return dirtyValidator(requiredIf(predicate));
|
|
}
|
|
|
|
/**
|
|
* Equivalent to dirtyValidator(Validator.required), however using dirtyValidator returns a new function
|
|
* each time which prevents formControl.hasError from properly comparing functions for equality.
|
|
*/
|
|
export function dirtyRequired(control: AbstractControl): ValidationErrors | null {
|
|
return control.dirty ? Validators.required(control) : null;
|
|
}
|