( control: FormControl | null, dir: NgControl, validateControlPresenceOnChange: boolean = true, )
| 120 | * to avoid potentially breaking changes caused by better control cleanup introduced in #39235. |
| 121 | */ |
| 122 | export function cleanUpControl( |
| 123 | control: FormControl | null, |
| 124 | dir: NgControl, |
| 125 | validateControlPresenceOnChange: boolean = true, |
| 126 | ): void { |
| 127 | const noop = () => { |
| 128 | if (validateControlPresenceOnChange && (typeof ngDevMode === 'undefined' || ngDevMode)) { |
| 129 | _noControlError(dir); |
| 130 | } |
| 131 | }; |
| 132 | |
| 133 | // The `valueAccessor` field is typically defined on FromControl and FormControlName directive |
| 134 | // instances and there is a logic in `selectValueAccessor` function that throws if it's not the |
| 135 | // case. We still check the presence of `valueAccessor` before invoking its methods to make sure |
| 136 | // that cleanup works correctly if app code or tests are setup to ignore the error thrown from |
| 137 | // `selectValueAccessor`. See https://github.com/angular/angular/issues/40521. |
| 138 | |
| 139 | dir?.valueAccessor?.registerOnChange(noop); |
| 140 | dir?.valueAccessor?.registerOnTouched(noop); |
| 141 | |
| 142 | cleanUpValidators(control, dir); |
| 143 | |
| 144 | if (control) { |
| 145 | dir._invokeOnDestroyCallbacks(); |
| 146 | control._registerOnCollectionChange(() => {}); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | function registerOnValidatorChange<V>(validators: (V | Validator)[], onChange: () => void): void { |
| 151 | validators.forEach((validator: V | Validator) => { |
no test coverage detected