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>