2

I have a form with an unlimited cardinality field that creates an 'Add more' button to add more fields. I also have a separate pair of fields. One of these fields has a value prepopulated on starting a new form, the other field is another unlimited cardinality field with an 'Add more' button & is blank on page load. I have attached a Drupal Behavior jQuery function to a button that I have added in a custom module with hook_form_alter: this jQuery sets the value of the second of the pair of fields to the value of the first when the custom button is clicked. This works. And the 'Add More' buttons for the unlimited cardinality fields work as well. However, after the custom button has been clicked once, the 'Add More' button will no longer create a new field, & I get the following errors in my logs:

Apr 25 02:56:36 lucifer drupal: https://somesite.com|1366880196|php|111.111.111.111|https://somesite.com/system/ajax|https://somesite.com/node/add/inventory-stocking-ticket|1||Warning: implode(): Invalid arguments passed in form_error() (line 1649 of /hosting/somesite/com/htdocs/cms_sandbox/includes/form.inc).

Apr 25 02:56:41 lucifer drupal: https://somesite.com|1366880201|php|111.111.111.111|https://somesite.com/system/ajax|https://somesite.com/node/add/inventory-stocking-ticket|1||Notice: Undefined index: #ajax in ajax_form_callback() (line 379 of /hosting/somesite/com/htdocs/cms_sandbox/includes/ajax.inc).

Here is my code for the custom button:

$form['field_ending_weight_garbage']['und']['garbage_no_change'] = array(
  '#type' => 'button',
  '#value' => t('No Change'),
  '#attributes' => array('onclick' => 'return (false);'),
  '#name' => 'garbagenochange',
);

And the code for the behavior:

(function ($) {
  Drupal.behaviors.noChange = {
    attach: function (context, settings) {
     $('#edit-field-ending-weight-garbage-und-garbage-no-change').once('noChange', function() {
      $('#edit-field-ending-weight-garbage-und-garbage-no-change', context).click(function () {
     var a = $('#edit-field-starting-weight-garbage-und-0-value').val();
     var b = $('#edit-field-ending-weight-garbage-und-0-value').val();
     if (b == "")
     {
       $('#edit-field-ending-weight-garbage-und-0-value').val( a );
     }
     else
     {
       $('#edit-field-ending-weight-garbage-und-0-value').select();
     }
        return false;
      });
    });
   }
  };
}(jQuery));

Which is attached in an after_build function like so:

drupal_add_js(drupal_get_path('theme', 'corolla').'/js/inventoryNoChange.js');

I'm using jQuery 1.5 by means of jQuery Update module, uncompressed. What could be causing this?

Edit: I've managed to eliminate the first error by adding the custom button like this:

$form['field_starting_weight_garbage']['und'][0]['garbage_no_change'] = array(
  '#type' => 'button',
  '#value' => t('No Change'),
  '#attributes' => array('onclick' => 'return (false);'),
    '#limit_validation_errors' => array(),
  '#name' => 'garbagenochange',
);

So, it's on the first field in the pair rather than the second, & it's at $form['field_starting_weight_garbage']['und'][0]['garbage_no_change'] rather than $form['field_starting_weight_garbage']['und']['garbage_no_change'].

But the ajax error persists, & the 'Add Another' functionality is still broken. I should note that it only throws the error & fails seemingly randomly. I can add several extra fields with the 'Add Another' button, but then it breaks, throws the error, & won't add more fields.

I've determined that it is not the Behavior/jQuery that is breaking things. Simply adding the custom button with no behavior attached to it causes the problem. Still hoping someone can shed some light on this.

1

So I went with a different approach to achieve the same thing. Instead of adding a button using the '#button' or '#submit' property in hook_form_alter, which was causing the conflict, I added a button with the following code:

$form['field_starting_weight_garbage']['und'][0]['value']['#prefix'] = '<div>';
$form['field_starting_weight_garbage']['und'][0]['value']['#suffix'] = '<input type="submit" value="No Change" class="form-submit" id="garbagenochange"></div>';

& adjusted the Drupal Behavior to attach to this element (id="garbagenochange").

I still don't know exactly what was causing the conflict or how to fix it, but this does what I need, so I'll go this route.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.