Commit e51a0e05 authored by Boris Kocherov's avatar Boris Kocherov

checkValidity(): fix error and improve invalid state information

parent acc0d2b0
/*jslint nomen: true, maxlen: 200, indent: 2, maxerr: 100*/ /*jslint nomen: true, maxlen: 200, indent: 2, maxerr: 100*/
/*global window, document, URL, rJS, RSVP, jIO, tv4 */ /*global window, document, URL, rJS, RSVP, jIO, tv4, location */
(function (window, document, rJS, RSVP, jIO, tv4) { (function (window, document, location, rJS, RSVP, jIO, tv4) {
"use strict"; "use strict";
var render_object, var render_object,
expandSchema; expandSchema;
...@@ -1416,11 +1416,19 @@ ...@@ -1416,11 +1416,19 @@
return tv4.validateMultiple(json_d, g.props.schema[""]); return tv4.validateMultiple(json_d, g.props.schema[""]);
}) })
.push(function (validation) { .push(function (validation) {
var index, var i,
tasks = []; error_id,
error,
span,
tasks = [],
errors_block = g.element.querySelector("div.error-block");
if (errors_block) {
errors_block.parentNode.removeChild(errors_block);
}
g.element.querySelectorAll(".error").forEach(function (error_message) { g.element.querySelectorAll(".error").forEach(function (error_message) {
error_message.textContent = ""; error_message.textContent = "";
error_message.removeAttribute("id");
error_message.hidden = true; error_message.hidden = true;
}); });
...@@ -1433,37 +1441,58 @@ ...@@ -1433,37 +1441,58 @@
return false; return false;
}); });
} }
function print_error(message) { span = document.createElement("span");
span.setAttribute("class", "error");
span.textContent = "errors: ";
errors_block = document.createElement("div");
errors_block.setAttribute("class", "subfield error-block");
errors_block.appendChild(span);
function print_error(error, errorUid, errorId) {
return function (element) { return function (element) {
var id = element.id, var id = element.id,
error_message; error_message,
a = document.createElement("a");
a.setAttribute("href", "#" + errorUid);
a.text = errorId;
element.setAttribute("class", "error-input"); element.setAttribute("class", "error-input");
error_message = element.querySelector("#" + id.replace("/", "\\/") + " > .error"); error_message = element.querySelector("#" + id.replace("/", "\\/") + " > .error");
error_message.textContent = message; error_message.appendChild(a);
error_message.setAttribute("id", errorUid);
error_message.appendChild(document.createTextNode(error.message));
error_message.hidden = false; error_message.hidden = false;
a = document.createElement("a");
a.text = errorId;
a.setAttribute("data-error-link", "#" + errorUid);
a.setAttribute("class", "error-link");
errors_block.appendChild(a);
}; };
} }
for (index in validation.errors) { for (i = 0; i < validation.errors.length; i += 1) {
if (validation.errors.hasOwnProperty(index)) { error = validation.errors[i];
error_id = (i + 1).toString();
tasks.push( tasks.push(
g.getElementByPath(validation.errors[index].dataPath) g.getElementByPath(error.dataPath || "/")
.push(print_error(validation.errors[index].message)) .push(print_error(error, "error" + error_id, error_id))
); );
} }
}
for (index in validation.missing) { for (i = 0; i < validation.missing.length; i += 1) {
if (validation.missing.hasOwnProperty(index)) { error = validation.missing[i];
error_id = (i + 1).toString();
tasks.push( tasks.push(
g.getElementByPath(validation.missing[index].dataPath) g.getElementByPath(error.dataPath || "/")
.push(print_error(validation.missing[index].message)) .push(print_error(error, "missing" + error_id, error_id))
); );
} }
}
return RSVP.Queue() return RSVP.Queue()
.push(function () { .push(function () {
return RSVP.all(tasks); return RSVP.all(tasks);
}) })
.push(function () {
g.element.insertBefore(errors_block, g.element.firstChild);
})
.push(g.notifyInvalid.bind(g)) .push(g.notifyInvalid.bind(g))
.push(function () { .push(function () {
return false; return false;
...@@ -1631,8 +1660,14 @@ ...@@ -1631,8 +1660,14 @@
return this.selfRemove(evt); return this.selfRemove(evt);
} }
var button_list = this.props.add_buttons, var link = evt.target.getAttribute("data-error-link"),
button_list = this.props.add_buttons,
i; i;
if (link) {
location.href = link;
return;
}
for (i = 0; i < button_list.length; i = i + 1) { for (i = 0; i < button_list.length; i = i + 1) {
if (evt.target === button_list[i].element) { if (evt.target === button_list[i].element) {
return button_list[i].event(evt); return button_list[i].event(evt);
...@@ -1663,4 +1698,4 @@ ...@@ -1663,4 +1698,4 @@
return getFormValuesAsJSONDict(g); return getFormValuesAsJSONDict(g);
}); });
}(window, document, rJS, RSVP, jIO, tv4)); }(window, document, location, rJS, RSVP, jIO, tv4));
\ No newline at end of file \ No newline at end of file
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