Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Relevant JS snippet for add students validation.
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
You can’t perform that action at this time.