From decf20bdf4b07cba05a381b189b8a28d4b55792b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Tue, 30 Jul 2013 15:12:07 +0200 Subject: [PATCH] Define validator before use --- setup/scripts.js | 114 +++++++++++++++++++++++------------------------ 1 file changed, 57 insertions(+), 57 deletions(-) diff --git a/setup/scripts.js b/setup/scripts.js index 754f879e8d..c7e2fd1725 100644 --- a/setup/scripts.js +++ b/setup/scripts.js @@ -42,6 +42,63 @@ $(function () { // Form validation and field operations // +/** + * Calls server-side validation procedures + * + * @param {Element} parent input field in
or
+ * @param {String} id validator id + * @param {Object} values values hash {element1_id: value, ...} + */ +function ajaxValidate(parent, id, values) +{ + parent = $(parent); + // ensure that parent is a fieldset + if (parent.attr('tagName') != 'FIELDSET') { + parent = parent.closest('fieldset'); + if (parent.length === 0) { + return false; + } + } + + if (parent.data('ajax') !== null) { + parent.data('ajax').abort(); + } + + parent.data('ajax', $.ajax({ + url: 'validate.php', + cache: false, + type: 'POST', + data: { + token: parent.closest('form').find('input[name=token]').val(), + id: id, + values: $.toJSON(values) + }, + success: function (response) { + if (response === null) { + return; + } + + var error = {}; + if (typeof response != 'object') { + error[parent.id] = [response]; + } else if (typeof response.error != 'undefined') { + error[parent.id] = [response.error]; + } else { + for (var key in response) { + var value = response[key]; + error[key] = jQuery.isArray(value) ? value : [value]; + } + } + displayErrors(error); + }, + complete: function () { + parent.removeData('ajax'); + } + })); + + return true; +} + /** * Automatic form submission on change. */ @@ -120,63 +177,6 @@ $.extend(true, validators, { } }); -/** - * Calls server-side validation procedures - * - * @param {Element} parent input field in
or
- * @param {String} id validator id - * @param {Object} values values hash {element1_id: value, ...} - */ -function ajaxValidate(parent, id, values) -{ - parent = $(parent); - // ensure that parent is a fieldset - if (parent.attr('tagName') != 'FIELDSET') { - parent = parent.closest('fieldset'); - if (parent.length === 0) { - return false; - } - } - - if (parent.data('ajax') !== null) { - parent.data('ajax').abort(); - } - - parent.data('ajax', $.ajax({ - url: 'validate.php', - cache: false, - type: 'POST', - data: { - token: parent.closest('form').find('input[name=token]').val(), - id: id, - values: $.toJSON(values) - }, - success: function (response) { - if (response === null) { - return; - } - - var error = {}; - if (typeof response != 'object') { - error[parent.id] = [response]; - } else if (typeof response.error != 'undefined') { - error[parent.id] = [response.error]; - } else { - for (var key in response) { - var value = response[key]; - error[key] = jQuery.isArray(value) ? value : [value]; - } - } - displayErrors(error); - }, - complete: function () { - parent.removeData('ajax'); - } - })); - - return true; -} - // // END: Form validation and field operations // ------------------------------------------------------------------