Trigger validation of all fields in Angular Form submit

后端 未结 13 1371
悲哀的现实
悲哀的现实 2020-12-08 01:45

I\'m using this method: http://plnkr.co/edit/A6gvyoXbBd2kfToPmiiA?p=preview to only validate fields on blur. This works fine, but I would also like to validate them (and thu

13条回答
  •  死守一世寂寞
    2020-12-08 02:18

    Based on Thilak's answer I was able to come up with this solution...

    Since my form fields only show validation messages if a field is invalid, and has been touched by the user I was able to use this code triggered by a button to show my invalid fields:

    // Show/trigger any validation errors for this step
    angular.forEach(vm.rfiForm.stepTwo.$error, function(error) {
      angular.forEach(error, function(field) {
        field.$setTouched();
      });
    });
    // Prevent user from going to next step if current step is invalid
    if (!vm.rfiForm.stepTwo.$valid) {
      isValid = false;
    }
    
    
    提交评论

提交回复
热议问题