AngularJS: list all form errors

前端 未结 4 734
执念已碎
执念已碎 2021-01-29 23:45

Background: I am currently working on an application with tabs; and I\'d like to list the fields / sections that fail validation, to direct the user to look for errors in the r

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-30 00:10

    Brett DeWoody's answer is correct. I wanted to do the logic in my controller though. So I wrote the below, which is based off of the answer user5045936 gave. This may also help some of you who want to go the controller route. By the way Im using the toaster directive to show my users validation messages.

     if (!vm.myForm.$valid) {
                var errors = [];
    
                for (var key in vm.myForm.$error) {
                    for (var index = 0; index < vm.myForm.$error[key].length; index++) {
                        errors.push(vm.myForm.$error[key][index].$name + ' is required.');
                    }
                }
    
                toaster.pop('warning', 'Information Missing', 'The ' + errors[0]);
    
                return;
            }
    

提交回复
热议问题