3

I set up a form to validate on the server side OnBlur event. The server-side validation is working fine and returning errors as expected. However, once I set validity to false for a field, setting it back to true is not removing the $error messages. Isn't $setValidity true supposed to remove errors from form?

This is the controller:

angular.module('artists').controller('ArtistsController', ['$scope', '$stateParams', '$location', 'Authentication', 'Artists',
  function($scope, $stateParams, $location, Authentication, Artists) {
    $scope.authentication = Authentication;

    $scope.processForm = function(val){

        var artist = new Artists({
            name: $scope.artistForm.name.$viewValue,
            quote: $scope.artistForm.quote.$viewValue
        });
        artist.$save(function(response) {
          $scope.artistForm.$setValidity(val,true);
        }, function(errorResponse) {
          if(val in errorResponse.data){
            $scope.artistForm.$setValidity(val,false,errorResponse.data[val].message);
          }else{
            $scope.artistForm.$setValidity(val,true);
          }
        });

    };
 }]);
hunch_hunch
  • 2,283
  • 1
  • 21
  • 26
BassMHL
  • 8,523
  • 9
  • 50
  • 67

1 Answers1

1

In my case the field is set back to normal, but when I edit it. That happens because the $scope is loaded from different controller and is not applied. Therefore, you need to use $apply

$scope.$apply(function(){
     $scope.artistForm.$setValidity(val,true);
})

Here's the docs: https://docs.angularjs.org/api/ng/type/$rootScope.Scope

mate.gvo
  • 1,093
  • 14
  • 20