# HG changeset patch # User Nathan Phillip Brink # Date 2010-11-02 01:07:42 # Node ID 1dcff9f4286f4c0d3b0128165c3feb5947879648 # Parent 59ee50e4d13c065ff3f7fd90f08c8730c2f7ad68 Fix up validation to not require a class/course name if the class/course has had not sections added to it. 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('
'); + jQuery('#jsrows').append('
'); jQuery('.className' + classNum).autocomplete({ source: "auto.php" }); jQuery('.className' + classNum).bind('autocompleteselect', {'class_num': classNum}, @@ -274,7 +311,6 @@ jQuery(document).ready(function() { //-------------------------------------------------- // Validates the form (pre-submission check) //-------------------------------------------------- - /* don't call if IE doesn't think the function exists */ jQuery('#scheduleForm').validate({ debug: false }); //--------------------------------------------------