Trigger validation of all fields in Angular Form submit

后端 未结 13 1398
悲哀的现实
悲哀的现实 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:20

    I know, it's a tad bit too late to answer, but all you need to do is, force all forms dirty. Take a look at the following snippet:

    angular.forEach($scope.myForm.$error.required, function(field) {
        field.$setDirty();
    });
    

    and then you can check if your form is valid using:

    if($scope.myForm.$valid) {
        //Do something
    }   
    

    and finally, I guess, you would want to change your route if everything looks good:

    $location.path('/somePath');
    

    Edit: form won't register itself on the scope until submit event is trigger. Just use ng-submit directive to call a function, and wrap the above in that function, and it should work.

提交回复
热议问题