2016-08-11 22:05:23 +08:00
|
|
|
/*
|
|
|
|
* Define AJAX methods for handling errors on forms.
|
|
|
|
*/
|
2016-08-05 23:00:29 +08:00
|
|
|
|
2016-08-11 22:05:23 +08:00
|
|
|
/*
|
2017-05-09 23:13:47 +08:00
|
|
|
* Render errors specified in array of strings format (or string if
|
|
|
|
* just one error) for a single form element.
|
|
|
|
*
|
|
|
|
* Show error message/s and mark error input (if errMsgs is defined)
|
|
|
|
* and, if present, mark and show the tab where the error occured and
|
|
|
|
* focus/scroll to the error input, if it is the first one to be
|
|
|
|
* specified or if errMsgs is undefined.
|
|
|
|
*
|
|
|
|
* @param {string} errAttributes Span element (error) attributes
|
|
|
|
* @param {boolean} clearErr Set clearErr to true if this is the only
|
|
|
|
* error that can happen/show.
|
|
|
|
*/
|
|
|
|
var renderFormError = function(ev, input, errMsgs, clearErr, errAttributes) {
|
2016-09-08 18:43:31 +08:00
|
|
|
clearErr = _.isUndefined(clearErr) ? false : clearErr;
|
2017-05-09 23:13:47 +08:00
|
|
|
errAttributes = _.isUndefined(errAttributes) ? '' : ' ' + errAttributes;
|
|
|
|
var $form = $(input).closest('form');
|
2016-09-08 18:43:31 +08:00
|
|
|
|
2016-08-05 23:00:29 +08:00
|
|
|
if (!_.isUndefined(errMsgs)) {
|
2016-09-08 18:43:31 +08:00
|
|
|
if (clearErr) {
|
|
|
|
$form.clearFormErrors();
|
|
|
|
}
|
|
|
|
|
2016-08-05 23:00:29 +08:00
|
|
|
// Mark error form group
|
2017-05-09 23:13:47 +08:00
|
|
|
var $formGroup = $(input).closest('.form-group');
|
|
|
|
if (!$formGroup.hasClass('has-error')) {
|
|
|
|
$formGroup.addClass('has-error');
|
2016-07-22 19:29:01 +08:00
|
|
|
}
|
2016-08-05 23:00:29 +08:00
|
|
|
|
|
|
|
// Add error message/s
|
2017-05-09 23:13:47 +08:00
|
|
|
var errorText = ($.makeArray(errMsgs).map(function(m) {
|
2016-08-05 23:00:29 +08:00
|
|
|
return m.strToErrorFormat();
|
2017-05-09 23:13:47 +08:00
|
|
|
})).join('<br />');
|
|
|
|
var $errSpan = "<span class='help-block'" +
|
|
|
|
errAttributes + '>' + errorText + '</span>';
|
2016-08-05 23:00:29 +08:00
|
|
|
$formGroup.append($errSpan);
|
2016-07-18 19:16:41 +08:00
|
|
|
}
|
|
|
|
|
2017-05-09 23:13:47 +08:00
|
|
|
var $parent;
|
|
|
|
var $tab = $(input).closest('.tab-pane');
|
2016-07-22 19:29:01 +08:00
|
|
|
if ($tab.length) {
|
2016-08-05 23:00:29 +08:00
|
|
|
// Mark error tab
|
2016-07-18 19:16:41 +08:00
|
|
|
tabsPropagateErrorClass($form);
|
|
|
|
$parent = $tab;
|
2016-07-14 00:47:06 +08:00
|
|
|
} else {
|
2016-07-18 19:16:41 +08:00
|
|
|
$parent = $form;
|
2016-07-14 00:47:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Focus and scroll to the error if it is the first (most upper) one
|
2017-05-09 23:13:47 +08:00
|
|
|
if ($parent.find('.form-group.has-error').length === 1 ||
|
|
|
|
_.isUndefined(errMsgs)) {
|
2016-08-05 23:00:29 +08:00
|
|
|
goToFormElement(input);
|
2016-07-14 00:47:06 +08:00
|
|
|
}
|
|
|
|
|
2017-05-09 23:13:47 +08:00
|
|
|
if (!_.isUndefined(ev)) {
|
2016-08-05 23:00:29 +08:00
|
|
|
// Don't submit form
|
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
2017-06-05 20:09:04 +08:00
|
|
|
ev.stopImmediatePropagation();
|
2016-08-05 23:00:29 +08:00
|
|
|
}
|
2017-05-09 23:13:47 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Render errors specified in JSON format for many form elements.
|
|
|
|
*/
|
|
|
|
$.fn.renderFormErrors = function(modelName, errors, clear, ev) {
|
2017-06-05 20:09:04 +08:00
|
|
|
clear = _.isUndefined(clear) ? true : clear;
|
|
|
|
if (clear) {
|
2017-05-09 23:13:47 +08:00
|
|
|
this.clearFormErrors();
|
|
|
|
}
|
2017-06-05 20:09:04 +08:00
|
|
|
var $form = $(this);
|
2017-05-09 23:13:47 +08:00
|
|
|
$.each(errors, function(field, messages) {
|
|
|
|
// Special exception for file uploads in steps and results
|
|
|
|
if (field === 'assets.file') {
|
|
|
|
field = 'assets_attributes';
|
|
|
|
} else if (field === 'asset.file') {
|
|
|
|
field = 'asset_attribute';
|
|
|
|
}
|
|
|
|
var types = 'input, file, select, textarea';
|
2017-06-05 20:09:04 +08:00
|
|
|
var $input = $(_.filter($form.find(types), function(el) {
|
2017-05-09 23:13:47 +08:00
|
|
|
var name = $(el).attr('name');
|
|
|
|
if (name) {
|
|
|
|
return name.match(new RegExp(modelName + '\\[' + field + '\\(?'));
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}));
|
|
|
|
|
|
|
|
renderFormError(ev, $input, messages);
|
|
|
|
});
|
|
|
|
};
|
2016-07-14 00:47:06 +08:00
|
|
|
|
2016-08-11 22:05:23 +08:00
|
|
|
/*
|
|
|
|
* If any of form tabs (if exist) has errors, mark it and
|
|
|
|
* and show the first erroneous tab.
|
|
|
|
*/
|
2016-07-22 19:29:01 +08:00
|
|
|
function tabsPropagateErrorClass($form) {
|
2017-05-09 23:13:47 +08:00
|
|
|
var $contents = $form.find('div.tab-pane');
|
|
|
|
_.each($contents, function(tab) {
|
2016-07-18 19:16:41 +08:00
|
|
|
var $tab = $(tab);
|
2017-05-09 23:13:47 +08:00
|
|
|
var $errorFields = $tab.find('.has-error');
|
2016-07-22 19:29:01 +08:00
|
|
|
if ($errorFields.length) {
|
2017-05-09 23:13:47 +08:00
|
|
|
var id = $tab.attr('id');
|
2016-07-22 19:29:01 +08:00
|
|
|
var navLink = $form.find("a[href='#" + id + "'][data-toggle='tab']");
|
|
|
|
if (navLink.parent().length) {
|
2017-05-09 23:13:47 +08:00
|
|
|
navLink.parent().addClass('has-error');
|
2016-02-12 23:52:43 +08:00
|
|
|
}
|
2016-07-18 19:16:41 +08:00
|
|
|
}
|
|
|
|
});
|
2017-05-09 23:13:47 +08:00
|
|
|
$form.find('.nav-tabs .has-error:first > a', $form).tab('show');
|
2016-02-12 23:52:43 +08:00
|
|
|
}
|