0

I have an angular directive used as attribute for validating the input.

Input looks like this (jade)

 input.form-control(type='text',  name='subjectinput', id='subjectinput',  subjectid-noprefix-validator='(subject.nosuffix==1)')

and validator looks like

.directive('subjectidNoprefixValidator', function () {     
    return {
        require: ['^form', 'ngModel'],
        scope: {
            noprefixformat: '=subjectidNoprefixValidator'
        },
        link: function (scope, element, attrs, ctrls) {

            scope.form = ctrls[0]; // so we can use form reference in displaying error messages
            var ngModel = ctrls[1];
            ngModel.$validators.subjectidnoprefix = function (value) { 
                var status = true;
                // removed some logic that sets status true or false
                // using   $scope.noprefixformat parameter
                return status;
            };
        }
    };
});

It works fine. However, sometimes in page actions an event happens that need validation to be re-triggered on the INPUT without touching it; some radio button alteration should re-trigger it and refresh validation status (as you see, my validator has a parameter).

I tried to achieve it by calling in page Controller

 $scope.subjCreationForm.subjectinput.$validate(); 

but it does not seem to work. What could be the case and is there another way to trigger validation of a field programmatically when it employs $validators?

1

1 Answer 1

0

Found the core of my problem, $apply has to be called manually that can be achieved as such:

           $timeout(function () {
                    $scope.subjCreationForm.subjectinput.$validate();
                });
Sign up to request clarification or add additional context in comments.

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.