jpillora / jpillora/jquery.async.validator

Prevalidate still shows initial prompts with multiple failing fields.

Open
#5 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
1
Forks
0
PR merge metrics
No merged PRs in 30d

Description

If you have several fields that fail during prevalidation, the subsequent ones will still get a prompt. This is with stock standard required checks without any groups.

Excerpt:

asyncValidator: FormExecution: 76f3: ValidationForm: form: exec fields #2
asyncValidator: RuleExecution: e14e: ValidationField: energy_response[field1]: Failed
asyncValidator: FieldExecution: c005: ValidationField: energy_response[field1]: Failed
asyncValidator: FormExecution: 76f3: ValidationForm: form: Failed
asyncValidator: RuleExecution: ece5: ValidationField: energy_response[field2]: Failed
asyncValidator: FieldExecution: b011: ValidationField: energy_response[field2]: Failed

This is due to parallelize rejecting as soon as one task fails. The other tasks aren't aborted, and so continue after showPrompt has been turned back on.

Note this code:

this.validate(function() {
    opts.showPrompt = true;
});

If you wanted a boolean result derived from AND logic, I'd expect to just abort the other tasks. But for a prevalidate determining the status of all fields it doesn't seem right to do that.

As such I made the fail logic pretty much the same as the success logic and this fixes the problem. Not thoroughly tested but my final code was:

diff --git a/vendor/assets/javascripts/jquery.async.validator.prompt.js b/vendor/assets/javascripts/jquery.async.validator.prompt.js
index f79c14b..7574627 100644
--- a/vendor/assets/javascripts/jquery.async.validator.prompt.js
+++ b/vendor/assets/javascripts/jquery.async.validator.prompt.js
@@ -452,20 +452,26 @@ $.Deferred.parallelize = function(fns) {

   var d = $.Deferred(),
       n = 0, i = 0, l = fns.length,
-      rejected = false;
+      rejection = null;

   if(!$.isArray(fns) || l === 0)
     return d.resolve();

   function pass(result) {
-    n++;
-    if(n === l) d.resolve(result);
+    maybe_resolve();
   }

   function fail(result) {
-    if(rejected) return;
-    rejected = true;
-    d.reject(result);
+    if (rejection == null) rejection = result;
+    maybe_resolve();
+  }
+
+  function maybe_resolve() {
+    n++;
+    if(n === l) {
+      if (rejection == null) d.resolve();
+      else d.reject(rejection);
+    }
   }

   //execute all at once

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in vendor/assets/javascripts/jquery.async.validator.prompt.js at $.Deferred.parallelize and inspect how pass and fail count completed tasks. Verify that all parallel validations finish before the combined result is settled, while retaining the first rejection. Done means multiple prevalidation failures no longer re-enable prompts for later fields, and the overall rejection behavior remains intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, jquery
Domain
frontend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.