Files
rkandleonsenft f9cf82642c fix(forms): apply validator directives to custom form controls
Custom control (`value` or `checked` model, no ControlValueAccessor) bound with NgModel,
FormControlDirective or FormControlName is set up by setupCustomControl, which doesnt call
setUpValidators. Validator directives on element (required, minlength, any directive providing
NG_VALIDATORS) and NG_VALIDATORS provided by control itself never reach FormControl, so control
stays valid and form submits. ControlValueAccessor path merges them, and cleanUpControl already
removes them when FormControlDirective or FormControlName lets go of control

Call setUpValidators from setupCustomControl. Applying validators needs few more changes:

- FormControlName: ɵngControlUpdate runs before ngOnChanges of element, so setting control up
  there would run control's own NG_VALIDATORS before its ngOnInit, and first ngOnChanges of each
  validator directive would emit valueChanges and statusChanges during first change detection.
  ɵngControlUpdate now only resolves control and ngOnChanges sets it up, same as for
  ControlValueAccessor
- required: with FormControlDirective and FormControlName, NgControl also wrote `required` into
  RequiredValidator on same element. That cleared static `required`, and Validators.required
  turned into directive validator that outlived removeValidators(). setInputOnDirectives now
  passes directive to writePredicate and NgControl skips RequiredValidator. Custom control gets
  `required` from enabled RequiredValidator or Validators.required under all three directives
- errors: status inputs are read through untracked getters and `errors` was plain field, so
  re-validation later in same pass (validator directive's ngOnChanges) left `invalid` and `errors`
  stale, and markForCheck during that refresh doesnt re-run view. `errors` is backed by signal now
  like `status`, and ngControlUpdate reads both, so errors-only change is bound in same pass, with
  `emitEvent: false` too
- _enabled on validator directives is backed by signal too, so change from their ngOnChanges
  reaches custom control in same change detection

Fixes #70721

Co-authored-by: leonsenft <[email protected]>
2026-11-11 14:59:21 -08:00
..