1

I have a custom validation, for example, blacklisted. My model value and blacklisted array are the following:

model = "not_blacklisted_yet"
blacklisted = ["foo", "bar"]

I want to add "not_blacklisted_yet" to blacklisted, and to fire the validation programmatically. If I just add the element, the validation is not triggered because I have not changed the models viewValue.

How can I achieve this?

EDIT, atach plunker: http://plnkr.co/edit/L2sJY9VOJ7s8lKCm88sM?p=preview

3
  • 1
    you can have a look at $scope.$watch and watch blacklisted to trigger the validation you are talking about Commented Jun 17, 2013 at 14:33
  • done, plnkr.co/edit/L2sJY9VOJ7s8lKCm88sM?p=preview Commented Jun 17, 2013 at 15:15
  • Did you ever get a working solution? Commented Jan 13, 2014 at 3:35

1 Answer 1

1

I agree with what @Atrix1987 said in his comment. Your requirements need two entry points to trigger the said validation - one is when the form controller's modelValue changes and the other is when the blacklisted changes. The former has already been taken care of by the use of ui-validation, so that leaves us to deal with the latter scenario.

We want to trigger the validation whenever blacklisted changes, so we need to monitor it via the use of $watch. In your controller add the following:

$scope.$watch('blacklisted', function(content) {
  if (content) {
    $scope.form.model.$setValidity(
      'blacklist',
      $scope.notBlackListed($scope.form.model.$modelValue)
    );
  }
}, true);

Or here is the plunkr

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.