Skip to content

Commit

Permalink
#8 Chybná AJAX validace při načtení stránky
Browse files Browse the repository at this point in the history
- Metoda `toggleControl` při validaci AJAXových pravidel použije správnou URL.
- Drobný refactor přetěžování `Nette` metod a ukládání referencí na původní metody.
  • Loading branch information
zipper committed Apr 11, 2019
1 parent 4b429f2 commit 0698c70
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ $ cat bower.json
## Changelog

### v1.3.8
- Stisknutím klávesy ESC v průběhu AJAXové validace nedojde k jejímu zrušení.
- Stisknutím klávesy ESC v průběhu AJAXové validace nedojde k jejímu zrušení ([#10](https://github.com/peckadesign/pdForms/issues/10)).
- Do globálního objektu `pdForms` přidána property `version` obsahující aktuální verzi.
- Metoda `toggleControl` při validaci AJAXových pravidel použije správnou URL ([#8](https://github.com/peckadesign/pdForms/issues/8)).
- Drobný refactor přetěžování `Nette` metod a ukládání referencí na původní metody.

### v1.3.7
- Oprava mazání zpráv v případě, že input má asynchronní i klasická pravidla dohromady. Za určitých okolností stále mohlo dojít k zobrazení dvou chyb zároveň, protože nedošlo ke smazání zprávy z asynchronního pravidla.
Expand Down
59 changes: 46 additions & 13 deletions pdForms.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ var pdForms = pdForms || {};
pdForms.version = '1.3.8';


/**
* Nette methods which are later overridden
*/
pdForms.Nette = {
validateControl: Nette.validateControl,
validateRule: Nette.validateRule,
toggleControl: Nette.toggleControl,
initForm: Nette.initForm
};


/**
* Constants for messages. Used in CSS class names.
*/
Expand Down Expand Up @@ -63,6 +74,23 @@ pdForms.isRuleOptional = function(rule) {
};


/**
* Method converts rules from Pd format into Nette compatible format. It effectively means flattening rule.arg structure
* by removing rule.arg.optional and assigning rule.arg.data into rule.arg
* @param rules
*/
pdForms.normalizeRulesArg = function(rules) {
for (var j in rules) {
rules[j].optional = pdForms.isRuleOptional(rules[j]);
if ('arg' in rules[j] && typeof rules[j].arg === 'object' && 'data' in rules[j].arg) {
rules[j].arg = rules[j].arg.data;
}
}

return rules;
};


/**
* Validates ever-focused inputs, using data-nette-rules and data-pd-rules. Validates all grouped elements together.
* This function is not used when validating whole form, eg. by submit event.
Expand Down Expand Up @@ -118,7 +146,7 @@ pdForms.validateControl = function(elem, rules, onlyCheck) {
}

var condition = !!rules[id].rules;
var valid = tmp_Nette_validateControl(elem, [rules[id]], ! condition || onlyCheck);
var valid = pdForms.Nette.validateControl(elem, [rules[id]], ! condition || onlyCheck);

// if rule is async, then do not write any message
if (! async) {
Expand Down Expand Up @@ -505,9 +533,8 @@ Nette.addEvent = function(element, on, callback) {
/**
* Validates single rule. If there is no validator in Nette.validators, then try to use pdForms.validators.
*/
var tmp_Nette_validateRule = Nette.validateRule;
Nette.validateRule = function(elem, op, arg, value) {
var ret = tmp_Nette_validateRule(elem, op.substring(0, pdForms.namespace.length) === pdForms.namespace ? op.substring(pdForms.namespace.length) : op, arg, value);
var ret = pdForms.Nette.validateRule(elem, op.substring(0, pdForms.namespace.length) === pdForms.namespace ? op.substring(pdForms.namespace.length) : op, arg, value);

if (ret === null) {
op = pdForms.formatOperation(op);
Expand All @@ -528,7 +555,6 @@ Nette.validateRule = function(elem, op, arg, value) {
/**
*
*/
var tmp_Nette_validateControl = Nette.validateControl;
Nette.validateControl = function(elem, rules, onlyCheck) {

if (!elem.nodeName) { // RadioNodeList
Expand All @@ -537,24 +563,31 @@ Nette.validateControl = function(elem, rules, onlyCheck) {
rules = rules || Nette.parseJSON(elem.getAttribute('data-nette-rules'));

// no rules -> skip element validation
if (rules.length === 0)
if (rules.length === 0) {
return true;

for (var j in rules) {
rules[j].optional = pdForms.isRuleOptional(rules[j]);
if ('arg' in rules[j] && typeof rules[j].arg === 'object' && 'data' in rules[j].arg) {
rules[j].arg = rules[j].arg.data;
}
}

// convert arg property in rules into Nette compatible format
rules = pdForms.normalizeRulesArg(rules);

return pdForms.validateControl(elem, rules, onlyCheck);
};


/**
*
*/
Nette.toggleControl = function(elem, rules, success, firsttime, value) {
// convert arg property in rules into Nette compatible format
rules = pdForms.normalizeRulesArg(rules);

pdForms.Nette.toggleControl(elem, rules, success, firsttime, value);
};


/**
* Setup handlers.
*/
var tmp_Nette_initForm = Nette.initForm;
Nette.initForm = function (form) {
var $form = $(form);
var $submit = $form.find(':submit');
Expand All @@ -563,7 +596,7 @@ Nette.initForm = function (form) {
$form.off('.netteForms');
$inputs.off('.pdForms .netteForms');

tmp_Nette_initForm(form);
pdForms.Nette.initForm(form);

$inputs.on('focus.pdForms change.pdForms', function() {
$(this).data('ever-focused', true);
Expand Down

0 comments on commit 0698c70

Please sign in to comment.