1

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?

4
  • No. no fiddler any more Commented Apr 8, 2014 at 9:46
  • 2
    Then: No. no advice any more Commented Apr 8, 2014 at 9:48
  • what i have to do to get fiddler? I put console.log(attrs.validateMessageCharacter); but print only a number of character. Commented Apr 8, 2014 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. Commented Apr 8, 2014 at 10:29

1 Answer 1

2

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

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

1 Comment

Tnx a lot. you are fantastic :D

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.