2

I'm trying to add multiple validation directive on a model but it is not working. Individually they work but together they don't work.

I have 2 directives to an input to validate minimum and maximum value that can be entered in it.

<input type="text" min-val="2" max-val="5" ng-model="age" />

Here is the pluker link

Thanks!

1 Answer 1

3

Almost there.

You passed an anonymous function to $parsers and $formatters array:

modelCtrl.$formatters.push(function(value) {
  validator(value);
});

Inside the anonymous function you are making a call to your validator function, but you're not returning the result.

To remedy, return the function result as in:

modelCtrl.$formatters.push(function(value) {
  return validator(value);
});

modelCtrl.$parsers.push(function(value) {
  return validator(value);
});

To simplify your code you might want to use the function name directly as a variable reference (no need for anonymous function):

modelCtrl.$formatters.push(validator);
modelCtrl.$parsers.push(validator);

PLUNKER

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

1 Comment

Thanks! Works perfect! I thought attached plunker has correct code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.