I have created directives for form controls. For required if value is blank validation works fine but I want to set required for empty and "-1" so modified selectControlDir in controlDirective.js. But getting error ctrl is not defined.
Select control directive code
function selectControlDir()
{
return {
transclude: true,
restrict: 'E',
scope: {
ngModel: '=',
queObj: '='
},
template: '<div class="form-group">\n\
<label for="{{queObj._attributeName}}" class="col-sm-5 control-label">{{queObj._text}}</label>\n\
<div class="col-sm-6"><select {{queObj._attributeName}} ng-options="ans._value as ans._promptText for ans in queObj._answerOptions" ng-model="ngModel" ng-required="queObj._required" class="form-control {{queObj._pageAttributes.cssclass}}" name="{{queObj._attributeName}}" id="{{queObj._attributeName}}"></select>\n\
</div>'
,
link: function (scope, element, attrs)
{
if(angular.isUndefined(scope.ngModel))
{
scope.ngModel = scope.queObj._pageAttributes.defaultValue;
}
// add a parser that will process each time the value is
// parsed into the model when the user updates it.
ctrl.$parsers.unshift(function (value) {
if (value) {
// test and set the validity after update.
var valid = value.charAt(0) == '' || value.charAt(0) == '-1';
ctrl.$setValidity('required', valid);
}
// if it's valid, return the value to the model,
// otherwise return undefined.
return valid ? value : undefined;
});
}
};
}
What I am missing here? Please see plunker for complete code https://plnkr.co/edit/GA74YHNFxFb0ARg16Sjj?p=preview
If I remove this ctrl.$parsers.unshift code error gets off.
{{queObj._attributeName}}