1

Here is a angularjs directive to show multiple form validation errors.
directive code -

app.directive('validationErrors', function($compile) {
    return({
        link : function($scope, el, attr) {

            $scope.fld = attr.id;
            $scope.individualValidationErrors = [];
            var model = ((attr.ngModel).split('.'))[0];

            $scope.validationErrors = {};
            $scope.validationErrors[model] = {};
            $scope.validationErrors[model][$scope.fld] = "";

            var html = $compile(
                '<div id="error-{{fld}}" style="color:red;">'+
                    '<ul>' +
                        '<li ng-repeat="error in individualValidationErrors[fld]">'+
                            '{{error}}' +
                        '</li>' +
                    '</ul>' +
                '</div>'
            )($scope);
            $('input[id="'+$scope.fld+'"]').after(html);

            $scope.$watch('validationErrors',
                function(newV) {
                    $scope.fld = attr.id;
                    $scope.individualValidationErrors = [];

                    console.log(newV);
                    console.log($scope.validationErrors);

                    if ($scope.fld != undefined) {
                        $scope.individualValidationErrors[$scope.fld] = $scope.validationErrors[model][$scope.fld];
                        //console.log($scope.individualValidationErrors);
                    }
                },
                true
            );
        }
    });
});

Html code -

<form ng-submit="registration()">
    <input validation-errors="validationErrors" maxlength="50" type="text" id="first_name" ng-model="User.first_name">
    <input validation-errors="validationErrors" maxlength="50" type="text" id="last_name" ng-model="User.last_name">
    <input validation-errors="validationErrors" maxlength="50" type="text" id="email" ng-model="User.email">
    <input validation-errors="validationErrors" type="password" id="password"  ng-model="User.password">
    <input class="btn btn-info" type="submit" id="registration-sbmit" value="Submit">
</form>

the error of the last field of form overwrites all fields in the form and so it is not showing individual error for field.. $scope.validationErrors variable is set in controller which i want to $watch in directive.

1

2 Answers 2

1

I think your primary issue here is that you are overwriting $scope.validationErrors in your link function. The link function will be run for every directive on the page. They are also sharing the same $scope object. So you need to conditionally create $scope.validationErrors and conditionally add top-level keys to it:

if(!$scope.validationErrors)
    $scope.validationErrors = {};
if(!$scope.validationErrors[model])
    $scope.validationErrors[model] = {};
$scope.validationErrors[model][$scope.fld] = "";

That should at least clear up your issue where you are only getting the last item in $scope.validationErrors.

Sign up to request clarification or add additional context in comments.

Comments

0

You can do it like this:

$scope.$watch('[myproperty1,myproperty2,myproperty3]',function(nv,ov){
//do some stuff
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.