While testing the latest dev version for a new site I found that maxlength was not working for regular textfields and only working for textarea's. I also found that the counter did not update reliably.

The attached patch is how I addressed these 2 problems.

The biggest problem was that the JS behavior was overwriting the element and maxlength variables for every iteration of the loop, causing only the last field in a form to work correctly. I recently learned about this the hard why on another project. Ref: http://api.jquery.com/bind/ see the "Passing Event Data" section.

For some reason WinMerge generated the patch as if every line in the JS file changed. Maybe it's some sort of character set change causing this.

Comments

NaX’s picture

StatusFileSize
new4.96 KB

Please ignore the first patch. Here is the correct full patch.

aron novak’s picture

Priority: Normal » Major

This patch in fact fixes two bugs:
#858614: Maxlength breaks on multiple fields
#963352: Maxlength shows negative amount instead of zero
Already reported and patched.
Unfortunately neither of these patches were committed. I hope this one or the previous ones will be committed soon and then we can use out-of-the-box version of maxlength.

I have one question about your patch:

-  if (remainingCount < 0) {
-    field.val(field.val().substr(0, limit));
-    remainingCnt = 0;
-  }

Compared to:

+ if (remainingCount < 0) {
+    $(field).val($(field).val().substr(0, limit));    
+  } 

I think the remainingCount variable has to be set to 0 to not confuse the users with negative values. But at least your patch removes the typo, which is great, however i think that line is needed in a corrected form.

aron novak’s picture

StatusFileSize
new4 KB

Rerolled w/ the suggested change in #2

NaX’s picture

@Aron Novak
I agree with your suggestion.

I have since my last patch made another change to the top of Drupal.maxLength_limit to prevent JS error when working with some other contribute modules.

  // Build maxlength values and settings 
  var id = $(field).attr('id');
  // Skip form elements that do not exist. This sometimes occurs with more 
  // complex multi step form building modules like pageroute.
  if (id == undefined) {
    return;
  }
  var limit = Drupal.settings.maxlength[id];
aron novak’s picture

StatusFileSize
new4.18 KB

Rerolled w/ the suggested change in #4

aron novak’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.