added extra validation for integers, specifically for min/max, and for valid integer

added ng-min and ng-max directive, and made adjustments to question form for new directives
This commit is contained in:
Jared Tabor 2014-10-08 11:56:24 -04:00
parent b22c4cb56a
commit 1b8ebc57e4
2 changed files with 62 additions and 6 deletions

View File

@ -38,10 +38,14 @@ angular.module('SurveyMakerFormDefinition', [])
// '<input type="text" ng-model="survey_description" name="survey_description" id="survey_maker_survey_description" class="form-control ng-pristine ng-valid">'+
// '<div class="error api-error ng-binding" id="survey_maker-survey_description-api-error" ng-bind="survey_description_api_error"></div>'+
// '</div></div></div>'+
'<div class="col-sm-12"><label for="survey"><span class="label-text prepend-asterisk">Questions</span></label>'+
'<div id="survey_maker_question_area"></div><div id="finalized_questions"></div>'+
'<button style="display:none" type="button" class="btn btn-sm btn-primary" id="add_question_btn" ng-click="addNewQuestion()" aw-tool-tip="Create a new question" data-placement="top" data-original-title="" title="" disabled><i class="fa fa-plus fa-lg"></i> Add Question</button>'+
'<div id="new_question"></div></div></div></div>'
'<div class="col-sm-12">'+
'<label for="survey"><span class="label-text prepend-asterisk">Questions</span></label>'+
'<div id="survey_maker_question_area"></div>'+
'<div id="finalized_questions"></div>'+
'<button style="display:none" type="button" class="btn btn-sm btn-primary" id="add_question_btn" ng-click="addNewQuestion()" aw-tool-tip="Create a new question" data-placement="top" data-original-title="" title="" disabled><i class="fa fa-plus fa-lg"></i> Add Question</button>'+
'<div id="new_question"></div>'+
'</div>'+
'</div>'//</div>'
// label: 'Survey Name',
// type: 'text',
// addRequired: true,
@ -52,7 +56,7 @@ angular.module('SurveyMakerFormDefinition', [])
},
buttons: { //for now always generates <button> tags
// buttons: { //for now always generates <button> tags
// save: {
// ngClick: 'formSave()', //$scope.function to call on click, optional
// ngDisabled: true //Disable when $pristine or $invalid, optional
@ -61,6 +65,6 @@ angular.module('SurveyMakerFormDefinition', [])
// ngClick: 'formReset()',
// ngDisabled: true //Disabled when $pristine
// }
}
// }
});

View File

@ -89,6 +89,58 @@ angular.module('AWDirectives', ['RestServices', 'Utilities', 'AuthService', 'Job
}
};
})
.directive('ngMin', ['Empty', function (Empty) {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, elem, attr, ctrl) {
scope.$watch(attr.ngMin, function () {
ctrl.$setViewValue(ctrl.$viewValue);
});
var minValidator = function (value) {
var min = scope.$eval(attr.ngMin) || -Infinity;
if (!Empty(value) && Number(value) < min) {
ctrl.$setValidity('ngMin', false);
return undefined;
} else {
ctrl.$setValidity('ngMin', true);
return value;
}
};
ctrl.$parsers.push(minValidator);
ctrl.$formatters.push(minValidator);
}
};
}])
.directive('ngMax', ['Empty', function (Empty) {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, elem, attr, ctrl) {
scope.$watch(attr.ngMax, function () {
ctrl.$setViewValue(ctrl.$viewValue);
});
var maxValidator = function (value) {
var max = scope.$eval(attr.ngMax) || Infinity;
if (!Empty(value) && Number(value) > max) {
ctrl.$setValidity('ngMax', false);
return undefined;
} else {
ctrl.$setValidity('ngMax', true);
return value;
}
};
ctrl.$parsers.push(maxValidator);
ctrl.$formatters.push(maxValidator);
}
};
}])
// integer Validate that input is of type integer. Taken from Angular developer
// guide, form examples. Add min and max directives, and this will check
// entered values is within the range.