$ SetValidity form will revert to true does not work

I have set up a form to validate for the OnBlur event on the server side. Server side validation works fine and returns errors as expected. However, once I've set a value for the false

field, setting it back to true

doesn't remove the $ error messages. Is $ setValidity true to remove errors from the 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);
          }
        });

    };
 }]);

      

+3


source to share


1 answer


In my case, the field is back to normal, but when I edit it. This is because the $ area is loaded from another 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

0


source







All Articles