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*/
/*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";
var render_object,
expandSchema;
......@@ -1416,11 +1416,19 @@
return tv4.validateMultiple(json_d, g.props.schema[""]);
})
.push(function (validation) {
var index,
tasks = [];
var i,
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) {
error_message.textContent = "";
error_message.removeAttribute("id");
error_message.hidden = true;
});
......@@ -1433,37 +1441,58 @@
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) {
var id = element.id,
error_message;
error_message,
a = document.createElement("a");
a.setAttribute("href", "#" + errorUid);
a.text = errorId;
element.setAttribute("class", "error-input");
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;
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) {
if (validation.errors.hasOwnProperty(index)) {
tasks.push(
g.getElementByPath(validation.errors[index].dataPath)
.push(print_error(validation.errors[index].message))
);
}
for (i = 0; i < validation.errors.length; i += 1) {
error = validation.errors[i];
error_id = (i + 1).toString();
tasks.push(
g.getElementByPath(error.dataPath || "/")
.push(print_error(error, "error" + error_id, error_id))
);
}
for (index in validation.missing) {
if (validation.missing.hasOwnProperty(index)) {
tasks.push(
g.getElementByPath(validation.missing[index].dataPath)
.push(print_error(validation.missing[index].message))
);
}
for (i = 0; i < validation.missing.length; i += 1) {
error = validation.missing[i];
error_id = (i + 1).toString();
tasks.push(
g.getElementByPath(error.dataPath || "/")
.push(print_error(error, "missing" + error_id, error_id))
);
}
return RSVP.Queue()
.push(function () {
return RSVP.all(tasks);
})
.push(function () {
g.element.insertBefore(errors_block, g.element.firstChild);
})
.push(g.notifyInvalid.bind(g))
.push(function () {
return false;
......@@ -1631,8 +1660,14 @@
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;
if (link) {
location.href = link;
return;
}
for (i = 0; i < button_list.length; i = i + 1) {
if (evt.target === button_list[i].element) {
return button_list[i].event(evt);
......@@ -1663,4 +1698,4 @@
return getFormValuesAsJSONDict(g);
});
}(window, document, rJS, RSVP, jIO, tv4));
\ No newline at end of file
}(window, document, location, rJS, RSVP, jIO, tv4));
\ 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