Trigger validation of all fields in Angular Form submit

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

    You can try this:

    // The controller
    
    $scope.submitForm = function(form){
       		//Force the field validation
       		angular.forEach(form, function(obj){
       			if(angular.isObject(obj) && angular.isDefined(obj.$setDirty))
       			{ 
       				obj.$setDirty();
       			}
       		})
            
            if (form.$valid){
    		
    			$scope.myResource.$save(function(data){
    		     	//....
    			});
    		}
    }
    
    
      

提交回复
热议问题