Why registerOnValidatorChange in Validator instances overwrites current onChange handler?

Why registerOnValidatorChange in Validator instances overwrites current onChange handler?



There is Validator interface in Angular. There are a lot of predefined validators in Angular (like RequiredValidators and stuff).



Why registerOnValidatorChange in Validator instances overwrites current onChange handler?



Here is example of the code: https://github.com/angular/angular/blob/4.4.6/packages/forms/src/directives/validators.ts#L95



So what was the motivation?



The problem is: I am trying to watch changing required state of some control. I used registerOnValidatorChange for that. After that I tried it on another control (inside ng-form element) and I realised, that my watcher is not working. It is so, since ngForm redefined onChange handler by calling registerOnValidatorChange method again.


registerOnValidatorChange


ng-form


ngForm


registerOnValidatorChange






@AngularInDepth.com any ideas?

– Sharikov Vladislav
Mar 28 '18 at 16:08




1 Answer
1



You have to notify the form when a validator's internal state has changed.



When you create a validator as a directive. That directive can have @Input() bindings that provide data for calculating the validator result.


@Input()



If that @Input() binding is changed, then the form does not know that the validator has been modified.


@Input()



So registerOnValidatorChange is called to pass a callback function to the directive. You keep a reference to that callback and call it when ever the directive is changed. This tells the form to check the validator again.


registerOnValidatorChange



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)