I think the following is a bug, but it might turn out as a support request.

Consider the following code that depends on date_popup module (from the Date package). It seems that the date_popup that is added in the submit handler isn't processed resulting with a non-working field -- while the form added in the original form is working properly.

/**
 * Form builder.
 */
function foo_form(&$form_state) {
  $form = array();

  $form['select'] = array(
    '#type' => 'select',
    '#title' => t('Select'),
    '#options' => array(1, 2, 3),
    '#attributes' => array('class' => 'ctools-auto-submit'),
  );

  $form['static_date'] = array(
    '#type' => 'date_popup', // This element is processed properly. 
    '#title' => t('Start date static'),
    '#default_value' => '',
    '#date_format' => 'm/d/Y',
    '#weight' => 5,
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Apply'),
    '#attributes' => array('class' => 'ctools-use-ajax ctools-auto-submit-click'),
    '#weight' => 99,
  );

  ctools_add_js('ajax-responder');
  ctools_add_js('auto-submit');
  drupal_add_js('misc/jquery.form.js');

  $form['#prefix'] = '<div class="clear-block" id="example-form">';
  $form['#suffix'] = '</div>';

  return $form;
}

/**
 * Submit handler.
 */
function foo_form_submit(&$form, &$form_state) {

  $form['date'] = array(
    '#type' => 'date_popup', // This date popup isn't procesed.
    '#title' => t('Start date'),
    '#default_value' => '',
    '#date_format' => 'm/d/Y',
    '#weight' => 5,
  );
}

Comments

merlinofchaos’s picture

I think what happens here is the same problem I've got with wysiwyg, and I don't know how to fix, is that the submit trigger for the ajax event runs before whatever trigger date popup uses to move its data into the field, and so the value isn't sent.

I don't know how to ensure other triggers run first.

amitaibu’s picture

Status: Active » Needs review
StatusFileSize
new717 bytes
    // As some new form elements might have been added while processing the form
    // (e.g. the submit handler added elements), we re-build the form.
amitaibu’s picture

Title: Date popup element isn't processed properly in ctools_process_form() » New form element aren't processed properly in ctools_process_form()
StatusFileSize
new696 bytes

Same approach, but moving code a bit. The above example is working, but indeed I'm not sure if that the proper fix.
@merlinofchaos -- does your wyswig problem work with this patch?

merlinofchaos’s picture

If a submit handler adds new elements, you need to set the 'rebuild' flag to have the form rebuilt. That's how the core fapi is supposed to work. I don't want to move away from that.

The problem I'm seeing with wysiwyg is completely different and unrelated; I thought this was purely a javascript binding issue.

Instead there seems to be something odd going on in FAPI.

However, I'm 100% sure that doing another form_builder() call after form_builder() has already been run is wrong unless using the 'rebuild' setting.

merlinofchaos’s picture

Is the problem possibly this? http://drupal.org/node/831922

amitaibu’s picture

Category: bug » support
Status: Needs review » Fixed
StatusFileSize
new1.62 KB

Ok, I guess it was indeed more of a support issue after all ;)

I have solved this by settings the $form_state['rebuild'] in the submit handler and adding the new elements in the form itself. I've attached the code for others if they need.

Status: Fixed » Closed (fixed)

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