Skip to content

Commit

Permalink
netteForms.js: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Mar 29, 2017
1 parent 94c47d5 commit 841e5de
Showing 1 changed file with 13 additions and 21 deletions.
34 changes: 13 additions & 21 deletions src/assets/netteForms.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 841e5de

Please sign in to comment.