MCPcopy Index your code
hub / github.com/angular/angular / setUpValidators

Function setUpValidators

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

Source from the content-addressed store, hash-verified

183 * @param dir Directive instance that contains validators to be setup.
184 */
185export 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.

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

Used in the wild real call sites across dependent graphs

searching dependent graphs…