Setting errors on a FormGroup instance in Angular Reactive Forms not working as expected

£可爱£侵袭症+ 提交于 2019-12-06 08:07:27

So, thanks to @incognito's confirmation, I figured out where the problem was after further inspection.

setErrors, does set the errors property of the form group instance. However, the custom validator shown in my question doesn't explicitly return a value (i.e. a falsey undefined value). Looking into the code of the reactive forms module in angular, I found this method, which merges errors raised by various validators in this line, the method checks whether errors (undefined in my code snippet) is not equal to null. This condition (as inspected in its es5 version) evaluates to false, which causes the result to be null, thus ignoring the content of the errors property set in the code. The lesson I learned is: always return a value from custom validators, even if further nested FormGroup instances have their own custom logic for setting errors.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!