1

In a directive like below how can we set validation manually on other element passed through attributes in directive. Code below gives error otherField does not exist while it should take value of this variable and not directly name of this field in scope string.

app.directive('higherThan', [
  function() {

    var link = function($scope, $element, $attrs, ctrl) {

      var otherField = $attrs.otherField;
      $scope.otherField.$setValidity('lowerThan', true);
      //Other details of directive not kept for simplicity
    }

    return {
      require: 'ngModel',
      link: link
    };

  }
]);

HTML

<form name="main_form">
<label>From</label>

<input name="from_date" type="text" class="form-control" ng-model="date_range[0]" lower-than="date_range[1]" ng-required="true" close-text="Close"  />

<label>To</label>

<input name="to_date" type="text" class="form-control" ng-model="date_range[1]" higher-than="date_range[0]" ng-required="true" close-text="Close" other-field="from_date"/>

<span ng-show="main_form.to_date.$error.higherThan">From date should be lower than to date</span>
</form>
1
  • could you show us your html too? Commented Jul 4, 2015 at 10:49

2 Answers 2

1
var otherField = $attrs.otherField;
$scope.otherField.$setValidity('lowerThan', true);

There are a few of issues here:

1) var otherField is a string, not the actual input.

2) Once you've set var otherField you're not actually using it for anything.

3) There's no property $scope.otherField, which is why you're getting the error.

Even if you did $scope[otherField] this would error, as the ngModelController for from_date does not belong to $scope it belongs to main_form.

You should try using console.log($scope) to investigate.

It should be:

app.directive('higherThan', [
    function($parse) {

        var link = function($scope, $element, $attrs, ctrl) {

            var otherField = $parse($attrs.otherField)($scope);
            otherField.$setValidity('lowerThan', true);
            //Other details of directive not kept for simplicity
        }

        return {
            require: 'ngModel',
            link: link
        };

    }
]);


<form name="main_form">
  <label>From</label>
  <input name="from_date" type="text" class="form-control" ng-model="date_range[0]" lower-than="date_range[1]" ng-required="true" close-text="Close" />
  <label>To</label>
  <input name="to_date" type="text" class="form-control" ng-model="date_range[1]" higher-than="date_range[0]" ng-required="true" close-text="Close" other-field="main_form.from_date" />
  <span ng-show="main_form.to_date.$error.higherThan">From date should be lower than to date</span>
</form>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, was confused about parse stuff and converting string to variable names!
0

$setValidity should be called on NgModelController object:

var link = function($scope, $element, $attrs, ctrl) {

    var otherField = $attrs.otherField;
    ctrl.$setValidity('lowerThan', true);
    //Other details of directive not kept for simplicity
}

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.