diff --git a/scripts/scheduleInput.js b/scripts/scheduleInput.js --- a/scripts/scheduleInput.js +++ b/scripts/scheduleInput.js @@ -49,6 +49,42 @@ var sectionsOfClass = new Array(); "
Select a day!
" ); +/** + * Class name validation: only require a class name if it has at least + * one section. Backend throws out empty classes and it's more + * convenient if we can let the user have one extra, empty class. This + * is because we automatically add a new class each time we do an + * autofill to make the life of the user easier and less confusing. + */ +jQuery.validator.addMethod('classRequired', + function(value, element) + { + if (value.length) + return true; + + var css_classes = jQuery(element).attr('class'); + var cnum_pos = css_classes.indexOf('className'); + var cnum = css_classes.substr(cnum_pos + 'className'.length, css_classes.indexOf(' ', cnum_pos) - cnum_pos - 'className'.length) * 1; + if (cnum < 0 || cnum > classNum) + alert('JS error: ' + cnum + ' is an invalid class number.'); + + /* + * ignore the class with no + * sections. This only works when the + * class was added and the user _never_ + * clicked the Add Section button. Once + * the user clicks that button, he has + * to delete the class because of how + * our numbering works. + */ + if (!sectionsOfClass[cnum]) + return true; + + return false; + }, + 'Enter Class Name.
' + ); + //-------------------------------------------------- // Add validation rules //-------------------------------------------------- @@ -58,6 +94,7 @@ var sectionsOfClass = new Array(); jQuery.validator.addClassRules("daysRequired", { daysRequired: true }); +jQuery.validator.addClassRules('classRequired', { classRequired: true }); //-------------------------------------------------- @@ -193,7 +230,7 @@ function add_sections(cnum, data) function add_class_n(name) { sectionsOfClass[classNum] = 0; // Initialize at 0 - jQuery('#jsrows').append('