Created
March 21, 2016 15:13
-
-
Save onlyurei/69278828e5452f58d7b2 to your computer and use it in GitHub Desktop.
Relevant JS snippet for add students validation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (exports.addStudentsValidNames().length) { | |
exports.addStudentsCheckingUsernames(true); | |
$.post('/classroom/check', { | |
students: JSON.stringify(exports.addStudentsValidNames()) | |
}, function (data) { | |
var hasErrors; | |
data.each(function (i) { | |
i.transfer = ko.observable(null); | |
i.errorFields = []; | |
i.warningFields = []; | |
if (i.errors instanceof Array) { | |
i.errors.each(function (j, index) { | |
var fields = j.match(fieldsRex); | |
if (fields.length && !index) { | |
i.errors[index] = j.remove(fields[0]); | |
i.errorFields = JSON.parse(fields[0]).fields; | |
} | |
}); | |
} | |
if (i.warnings instanceof Array) { | |
i.warnings.each(function (j, index) { | |
var fields = j.match(fieldsRex); | |
if (fields.length && !index) { | |
i.warnings[index] = j.remove(fields[0]); | |
i.warningFields = JSON.parse(fields[0]).fields; | |
} | |
}); | |
} | |
i.firstName = ko.observable(i.firstName); | |
i.lastName = ko.observable(i.lastName); | |
i.username = ko.observable(((exports.addStudentsMode() == 'needUsername') && (reValidate === true)) ? i.suggestion : i.username); | |
i.password = ko.observable(i.password); | |
i.firstNameEditable = ko.observable(false); | |
i.lastNameEditable = ko.observable(false); | |
i.usernameEditable = ko.observable(false); | |
i.passwordEditable = ko.observable(false); | |
i.mode = exports.addStudentsMode(); | |
i.invalidate = function () { | |
exports.addStudentsHasPotentialErrors(true); | |
}; | |
i.editing = function (editing) { | |
exports.addStudentsEditingFields(editing); | |
}; | |
i.remove = function () { | |
var _data = exports.addStudentsNames(); | |
_data.remove(i); | |
hasErrors = _data.any(function (j) { | |
return j.errors.length; | |
}); | |
_data.each(function (j) { | |
j.hasErrors(hasErrors); | |
}); | |
exports.addStudentsNames.valueHasMutated(); | |
}; | |
}); | |
hasErrors = data.any(function (i) { | |
return i.errors.length; | |
}); | |
data.each(function (i) { | |
i.hasErrors = ko.observable(hasErrors); | |
}); | |
exports.addStudentsNames(data); | |
exports.addStudentsCheckingUsernames(false); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment