Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a validation custom form, I use validate-message-character="{{compose.limitCharacter - compose.message.length}}" in a select and textarea like this

            <form class="form-horizontal" role="form" name="composeForm" >

            <select ng-change="limitComposeCharacter()" ng-selected="compose.profile" 
             ng-model="compose.profile" ng-options="userId in profiles" name="profile" 
             validate-message-character="{{compose.limitCharacter - compose.message.length}}" required>
            </select>

               number Character: {{compose.limitCharacter - compose.message.length}}

            <textarea class="form-control" ng-model="compose.message" name="message" 
    validate-message-character="{{compose.limitCharacter - compose.message.length}}" 
required></textarea>
              Form validity: {{composeForm.$valid}}

I have something like this:

  • 1° Select User has compose.limitCharacter = 100
  • 2° Select User has compose.limitCharacter = 200 etc etc.

This is my directive to check number Character is > 0

angular.module('App')
  .directive('validateMessageCharacter', function () {
    return {
      require: 'ngModel',
      link: function postLink(scope, element, attrs, c) {
        scope.$watch(attrs.ngModel, function() {
            console.log(attrs.validateMessageCharacter);
            if(attrs.validateMessageCharacter < 0)
            {
                c.$setValidity('maxCharacter', false);
                c.$invalid = true;
            }else{
                c.$setValidity('maxCharacter', true);
                c.$invalid = false;
            }

        });
      }
    };
  });

It doesn't work proply when change select without change the textarea some advice?

share|improve this question
    
Some fiddle ??? –  ExpertSystem Apr 8 '14 at 9:33
    
No. no fiddler any more –  Silvio Troia Apr 8 '14 at 9:46
2  
Then: No. no advice any more –  ExpertSystem Apr 8 '14 at 9:48
    
what i have to do to get fiddler? I put console.log(attrs.validateMessageCharacter); but print only a number of character. –  Silvio Troia Apr 8 '14 at 10:20
1  
No, I said fiddle, as in jsfiddle.net (or plankr or jsBin or CodePen or whatever). You create a little app (basically an HTML file and a JS file) that contain all necessary components to demonstrate the problem. –  ExpertSystem Apr 8 '14 at 10:29

1 Answer 1

up vote 2 down vote accepted

First, using an advice from the angular google group, I changed the scope.$watch to attr.$observe. Second, the reason it validated only after typing text is that the text area is a required field. Your code works here: http://plnkr.co/edit/YvsuLoHzX9eqb7FhDgXA?p=preview

share|improve this answer
    
Tnx a lot. you are fantastic :D –  Silvio Troia Apr 8 '14 at 14:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.