title: angularjs 1.2 - validation directive update on $watch
body: <p>I have the following directive that I'm using for validation on a multi-select to allow for (dynamic) length validation of the number of selected items.</p>
  
  <pre><code>(function() {
      'use strict';
  
      angular
          .module('myModule')
          .directive('listLength', listLength);
  
      listLength.$inject = ['$parse'];
  
      function listLength($parse) {
          var directive = {
              require: 'ngModel',
              restrict: 'A',
              link: link
          };
  
          function link(scope, elem, attr, ngModel) {
              var length = 0;
              var exp = $parse(attr.listLength);
              if (exp.constant) {
                  // Single value, no need to watch
                  length = exp(scope);
              } else {
                  // We have an expression, need to watch for changes
                  scope.$watch(attr.listLength, function(newVal, oldVal) {
                      length = newVal;
                  });
              }
  
              //For DOM -&gt; model validation
              ngModel.$parsers.unshift(function(value) {
                  if (!angular.isUndefined(value) &amp;&amp; value !== "") {
                      var valid = value.length === length;
                      ngModel.$setValidity('listLength', valid);
                      return valid ? value : undefined;
                  }
                  return value;
              });
  
              //For model -&gt; DOM validation
              ngModel.$formatters.unshift(function(value) {
                  if (!angular.isUndefined(value) &amp;&amp; value !== "") {
                      var valid = value.length === length;
                      ngModel.$setValidity('listLength', valid);
                  }
                  return value;
              });
          }
  
          return directive;
      }
  })();
  </code></pre>
  
  <p>In order to have it update properly, I need to trigger the validation to be run when the expression (attr.listLength) updates, but it's not obvious to me how to achieve this. I tried setting <code>ngModel.$dirty</code> to true but it still does not update.</p>