(control: AbstractControl, dir: AbstractControlDirective)
| 183 | * @param dir Directive instance that contains validators to be setup. |
| 184 | */ |
| 185 | export function setUpValidators(control: AbstractControl, dir: AbstractControlDirective): void { |
| 186 | const validators = getControlValidators(control); |
| 187 | if (dir.validator !== null) { |
| 188 | control.setValidators(mergeValidators<ValidatorFn>(validators, dir.validator)); |
| 189 | } else if (typeof validators === 'function') { |
| 190 | // If sync validators are represented by a single validator function, we force the |
| 191 | // `Validators.compose` call to happen by executing the `setValidators` function with |
| 192 | // an array that contains that function. We need this to avoid possible discrepancies in |
| 193 | // validators behavior, so sync validators are always processed by the `Validators.compose`. |
| 194 | // Note: we should consider moving this logic inside the `setValidators` function itself, so we |
| 195 | // have consistent behavior on AbstractControl API level. The same applies to the async |
| 196 | // validators logic below. |
| 197 | control.setValidators([validators]); |
| 198 | } |
| 199 | |
| 200 | const asyncValidators = getControlAsyncValidators(control); |
| 201 | if (dir.asyncValidator !== null) { |
| 202 | control.setAsyncValidators( |
| 203 | mergeValidators<AsyncValidatorFn>(asyncValidators, dir.asyncValidator), |
| 204 | ); |
| 205 | } else if (typeof asyncValidators === 'function') { |
| 206 | control.setAsyncValidators([asyncValidators]); |
| 207 | } |
| 208 | |
| 209 | // Re-run validation when validator binding changes, e.g. minlength=3 -> minlength=4 |
| 210 | const onValidatorChange = () => control.updateValueAndValidity(); |
| 211 | registerOnValidatorChange<ValidatorFn>(dir._rawValidators, onValidatorChange); |
| 212 | registerOnValidatorChange<AsyncValidatorFn>(dir._rawAsyncValidators, onValidatorChange); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Cleans up sync and async directive validators on provided form control. |
no test coverage detected
searching dependent graphs…