MCPcopy Create free account
hub / github.com/angular/angular / setUpValidators

Function setUpValidators

packages/forms/src/directives/shared.ts:186–214  ·  view source on GitHub ↗
(control: AbstractControl, dir: AbstractControlDirective)

Source from the content-addressed store, hash-verified

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

Callers 3

_updateValidatorsFunction · 0.90
setUpFormContainerFunction · 0.85

Calls 6

getControlValidatorsFunction · 0.90
mergeValidatorsFunction · 0.90
setValidatorsMethod · 0.80
setAsyncValidatorsMethod · 0.80

Tested by

no test coverage detected