From 841e5deb11b7d2f15d53acd0f7313b851fd7d301 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 29 Mar 2017 18:36:41 +0200 Subject: [PATCH] netteForms.js: refactoring --- src/assets/netteForms.js | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/src/assets/netteForms.js b/src/assets/netteForms.js index 57de24d45..e54940f57 100644 --- a/src/assets/netteForms.js +++ b/src/assets/netteForms.js @@ -124,13 +124,18 @@ /** * Returns the effective value of form element. */ - Nette.getEffectiveValue = function(elem) { + Nette.getEffectiveValue = function(elem, filter) { var val = Nette.getValue(elem); if (elem.getAttribute) { if (val === elem.getAttribute('data-nette-empty-value')) { val = ''; } } + if (filter) { + var ref = {value: val}; + Nette.validateControl(elem, null, true, ref); + val = ref.value; + } return val; }; @@ -162,8 +167,7 @@ } curElem = curElem.tagName ? curElem : curElem[0]; // RadioNodeList - var curValue = elem === curElem ? value : {value: Nette.getEffectiveValue(curElem)}, - success = Nette.validateRule(curElem, rule.op, rule.arg, curValue); + var success = Nette.validateRule(curElem, rule.op, rule.arg, elem === curElem ? value : undefined); if (success === null) { continue; @@ -306,27 +310,13 @@ }; - /** - * Expand rule argument. - */ - Nette.expandRuleArgument = function(form, arg) { - if (arg && arg.control) { - var control = form.elements.namedItem(arg.control), - value = {value: Nette.getEffectiveValue(control)}; - Nette.validateControl(control, null, true, value); - arg = value.value; - } - return arg; - }; - - var preventFiltering = false; /** * Validates single rule. */ Nette.validateRule = function(elem, op, arg, value) { - value = value === undefined ? {value: Nette.getEffectiveValue(elem)} : value; + value = value === undefined ? {value: Nette.getEffectiveValue(elem, true)} : value; if (op.charAt(0) === ':') { op = op.substr(1); @@ -338,7 +328,10 @@ if (!preventFiltering) { preventFiltering = true; for (var i = 0, len = arr.length; i < len; i++) { - arr[i] = Nette.expandRuleArgument(elem.form, arr[i]); + if (arr[i] && arr[i].control) { + var control = elem.form.elements.namedItem(arr[i].control); + arr[i] = control === elem ? value.value : Nette.getEffectiveValue(control, true); + } } preventFiltering = false; } @@ -590,8 +583,7 @@ if (success !== false) { rule.neg = op[1]; rule.op = op[2]; - var curValue = elem === curElem ? value : {value: Nette.getEffectiveValue(curElem)}; - curSuccess = Nette.validateRule(curElem, rule.op, rule.arg, curValue); + curSuccess = Nette.validateRule(curElem, rule.op, rule.arg, elem === curElem ? value : undefined); if (curSuccess === null) { continue;