I'm trying to write a custom validator require-items that will yield valid/invalid for the input form field based on the length of an array in the scope...in this case skillTags
<input
type="text"
name="tags"
ng-model="newTag"
class="form-control"
placeholder="Enter tags: (eg. JavaScript, HTML5)"
ng-keyup="search($event)"
ng-focus="search($event)"
ng-class="{ 'has-results': matches.length }"
require-items="{{skillTags.length}}"
mongoose-error>
//custom validator not working
'use strict';
angular.module('offsiteApp')
.directive('requireItems', function (){
return {
require: 'ngModel',
link: function(scope, elem, attr, ngModel) {
var len = parseInt(attr.requireItems);
//For DOM -> model validation
ngModel.$parsers.unshift(function(value) {
var valid = len ? true : false;
ngModel.$setValidity('require-items', valid);
return valid ? value : undefined;
});
//For model -> DOM validation
ngModel.$formatters.unshift(function(value) {
var valid = len ? true : false;
ngModel.$setValidity('require-items', valid);
return value;
});
}
};
});
<p class="help"
ng-show="form.tags.$error['require-items'] && submitted">
Skill tags are required.
</p>