I'm getting the following JS error on the registration form:

Error: field.val() is undefined
Source File: http://gvep.localhost/sites/all/modules/contrib/maxlength/maxlength.js?R
Line: 7

I'm using maxlength on 2 profile fields that do not appear on the registration form. It seems this module still includes some JS in the page header to look out for these fields.

"maxlength": { "edit-field-organisation-info-0-value": "500", "edit-field-bio-0-value": "500" }

When it doesn't find them we get an error.

Comments

rsaddington’s picture

Title: JavaScript error "Error: field.val() is undefined" » JavaScript error "Error: field.val() is undefined" when using content profile
Assigned: Unassigned » rsaddington
Status: Active » Needs review
StatusFileSize
new1.15 KB

After a bit of investigation it seems the error is triggered when using maxlength on content profile fields and then hiding them from the registration form.

The following update to maxlength.module resolve the issue for me:

    // Only add JS on reg form if the field is not hidden...
    $settings = variable_get('content_profile_profile',array());
    
    if(arg(1) == 'register') {
      if(!in_array($element['#field_name'],$settings['registration_hide'])) {
        $js_settings = array(
          'maxlength' => array(
            'edit-'. $id => $values['limit'],
          ),
        );
      }
    } else {
      $js_settings = array(
        'maxlength' => array(
          'edit-'. $id => $values['limit'],
        ),
      );        
    }

The logic could perhaps be improved.

Patch file attached.

Orjan’s picture

I get this error on Firefox 3.6 and IE 8 in an add node form, but it works in chrome...

vladsavitsky’s picture

I had the same problem.
JS code is not check if element exists and this leads to errors.
I don't have time to do real patch and will do it later.
Here is my hack:

/* $Id: maxlength.js,v 1.1.6.6 2008/09/29 00:14:48 acm Exp $ */

Drupal.maxLength_limit = function (field) {
  if ($(field).length > 0) {
    console.log(field);
    var limit = field.attr("limit");

    // calculate the remaining count of chars
    var remainingCnt = limit - field.val().length;

    // if there is not remaining char, we clear additional content
    if (remainingCnt < 0) {
      field.val(field.val().substr(0, limit));
      remainingCnt = 0;
    }

    // update the remaing chars text
    $('#maxlength-'+field.attr('id').substr(5) + ' span.maxlength-counter-remaining').html(remainingCnt.toString());
  }
}

Drupal.maxLength_change = function () {
  var element = $(this);
  Drupal.maxLength_limit($(this), $(this).attr("limit"));
}

if (Drupal.jsEnabled) {
  $(document).ready(function(){
    // get all the settings, and save the limits in the fields
    for (var id in Drupal.settings.maxlength) {
      console.log("#" + id);

      var limit = Drupal.settings.maxlength[id];
      var element = $("#" + id);
      element.attr("limit", limit);
      // update the count at the page load
      Drupal.maxLength_limit(element);

      element.load(Drupal.maxLength_change);
      element.keyup(Drupal.maxLength_change);
      element.change(Drupal.maxLength_change);
    }
  });
}

Was added just one check: if ($(field).length > 0) {

vladsavitsky’s picture

Title: JavaScript error "Error: field.val() is undefined" when using content profile » [Patch] JavaScript error "Error: field.val() is undefined" when using content profile
Component: Miscellaneous » Code
Assigned: rsaddington » vladsavitsky
Status: Needs review » Patch (to be ported)
StatusFileSize
new953 bytes

Patch solves this problem. Please test it.

jpmckinney’s picture

Version: 6.x-2.0-beta1 » 6.x-2.x-dev
Status: Patch (to be ported) » Needs review
StatusFileSize
new796 bytes

Better patch, I think.

jpmckinney’s picture

StatusFileSize
new803 bytes

Forgot the .length.

aron novak’s picture

Status: Needs review » Fixed

I added the if() condition to the current JS, it makes the script much more robust.

Status: Fixed » Closed (fixed)

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