Commit 2f1e6966 by zubiar-arbi

add validation for grading types (allow only integers)

STUD-988
parent 3fc461f4
...@@ -23,5 +23,13 @@ define ["js/models/settings/course_grader"], (CourseGrader) -> ...@@ -23,5 +23,13 @@ define ["js/models/settings/course_grader"], (CourseGrader) ->
model = new CourseGrader() model = new CourseGrader()
errors = model.validate({min_count: 0, drop_count: ''}, {validate:true}) errors = model.validate({min_count: 0, drop_count: ''}, {validate:true})
expect(errors.min_count).toBe('Please enter an integer greater than 0.') expect(errors.min_count).toBe('Please enter an integer greater than 0.')
expect(errors.drop_count).toBe('Please enter an integer.') expect(errors.drop_count).toBe('Please enter non-negative integer.')
# don't allow negative integers
errors = model.validate({min_count: -12, drop_count: -1}, {validate:true})
expect(errors.min_count).toBe('Please enter an integer greater than 0.')
expect(errors.drop_count).toBe('Please enter non-negative integer.')
# don't allow floats
errors = model.validate({min_count: 12.2, drop_count: 1.5}, {validate:true})
expect(errors.min_count).toBe('Please enter an integer greater than 0.')
expect(errors.drop_count).toBe('Please enter non-negative integer.')
...@@ -51,19 +51,19 @@ var CourseGrader = Backbone.Model.extend({ ...@@ -51,19 +51,19 @@ var CourseGrader = Backbone.Model.extend({
}} }}
if (_.has(attrs, 'min_count')) { if (_.has(attrs, 'min_count')) {
var intMinCount = parseInt(attrs.min_count, 10); var intMinCount = parseInt(attrs.min_count, 10);
if (!isFinite(intMinCount) || /\D+/.test(intMinCount) || intMinCount < 1) { if (!isFinite(intMinCount) || /\D+/.test(attrs.min_count) || intMinCount < 1) {
errors.min_count = gettext("Please enter an integer greater than 0."); errors.min_count = gettext("Please enter an integer greater than 0.");
} }
else attrs.min_count = intMinCount; else attrs.min_count = intMinCount;
} }
if (_.has(attrs, 'drop_count')) { if (_.has(attrs, 'drop_count')) {
var intDropCount = parseInt(attrs.drop_count, 10); var intDropCount = parseInt(attrs.drop_count, 10);
if (!isFinite(intDropCount) || /\D+/.test(intDropCount) || isNaN(intDropCount)) { if (!isFinite(intDropCount) || /\D+/.test(attrs.drop_count) || isNaN(intDropCount) || intDropCount < 0) {
errors.drop_count = gettext("Please enter an integer."); errors.drop_count = gettext("Please enter non-negative integer.");
} }
else attrs.drop_count = intDropCount; else attrs.drop_count = intDropCount;
} }
if (_.has(attrs, 'min_count') && _.has(attrs, 'drop_count') && attrs.drop_count > attrs.min_count) { if (_.has(attrs, 'min_count') && _.has(attrs, 'drop_count') && !_.has(errors, 'min_count') && !_.has(errors, 'drop_count') && attrs.drop_count > attrs.min_count) {
errors.drop_count = _.template( errors.drop_count = _.template(
gettext("Cannot drop more <% attrs.types %> than will assigned."), gettext("Cannot drop more <% attrs.types %> than will assigned."),
attrs, {variable: 'attrs'}); attrs, {variable: 'attrs'});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment