1

I have a form button which I want to add another field with another button on that form( I am able to do this) but second button is not clickable. Any idea. Following is my code-

This is my form code-

function create_report_form($form, $form_state) {
$form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Report ttitle'),
    '#value' =>'',
);

$form['actions'] = array(
  '#type' => 'actions',
);

$form['actions']['submit'] = array(
  '#type' => 'submit',
  '#value' => t('Save'),
);
$form['conditions'] = array(
  '#tree' => TRUE,
);
$form['conditions']['report_type'] = array(
  '#title' => t('Report type'),
  '#type' => 'select',
  '#prefix'=> '<div class="conditions-wrapper"></div>',
  '#multiple' => false, 
  '#options' => get_report_type()
); 

 $form['conditions']['submit'] = array(
   '#type' => 'button',
   '#value' => t('Add condition'),
   //'#name' =>   '_add_another_outcome',
   '#ajax' => array(
      'callback' => 'reports_add_conditions',
      'wrapper' => 'report-form',
      'method' => 'replace',
      'effect' => 'fade',
   ),
  );  
   return $form;
}

Now, when I click on this button it will call the following function and generate another form element(select list) with another button which will call another ajax function (this is what which is not working).

function reports_add_conditions($form, &$form_state){
$values = $form_state['values'];
$form_state['rebuild'] = TRUE;
$type = $values['conditions']['report_type'];
$form['attributes'] = array(
  '#tree' => TRUE,
);
$form['attributes']['submit'] = array(
    '#type' => 'button',
    '#value' => t('Add attribute condition'),
    '#ajax' => array(
      'callback' => 'reports_add_attribute_conditions',
      'wrapper' => 'report-form',
      'method' => 'replace',
      'effect' => 'fade',
    ),
);       
$form['attributes']['attribute'] = array(
    '#type' => 'select',
    '#title' => '',
    '#options' => get_options($type),
    '#size' => 22, 
);  
  return drupal_render($form);
}

As you can see from the code I am not able to call "reports_add_attribute_conditions" function for the button.

This is my second call back code-

function reports_add_attribute_conditions($form, &$form_state){
$form_state['rebuild'] = TRUE;
$values = $form_state['values'];
$type = $values['conditions']['report_type'];
$form['attribute'] = array(
    '#type' => 'select',
    '#name' => $type,
    '#title' => '',
    '#options' =>  get_options($type),
    '#size' => 22, 
); 
$form['attributes']['submit'] = array(
    '#type' => 'button',
    '#value' => t('Add attribute condition'),
    '#ajax' => array(
      'callback' => 'reports_add_attribute_conditions',
      'wrapper' => 'report-form',
      'method' => 'replace',
      'effect' => 'fade',
    ),
); 

 return drupal_render($form);
}

Any idea is greatly appreciable

4
  • Are there errors in Drupal's logs, the browser JS console, or the web server's logs?
    – acrosman
    Commented Jun 28, 2016 at 17:36
  • no errors. nothing in console not even showing the callback URL. Commented Jun 28, 2016 at 17:38
  • Have you tried just returning the $form array instead of rendering it first. The docs imply it shouldn't matter, but I've always returned the render array or one of the framework commands: api.drupal.org/api/drupal/includes%21ajax.inc/group/…
    – acrosman
    Commented Jun 28, 2016 at 17:48
  • you mean just return $form; right. Its still not working. Commented Jun 28, 2016 at 17:51

2 Answers 2

1

@NabajitRoy my idea is

  1. Never use drupal_render() manual calls (return $form array as-is)
  2. Move all the form generation code into form constructor( create_report_form() ), AJAX callback should not create new elements for form but only create_report_form() should
  3. AJAX callbacks should only return element form (not generate them) e.g. return $form['attributes']['submit'] and may be $form_state['rebuild'] = true; if needed.
  4. To know in create_report_form() if it was called by AJAX request button check the $form_state['triggering_element'] in it.
  5. I feel that using '#type' => 'submit' instead of '#type' => 'button' is better

https://www.drupal.org/node/752056 https://www.drupal.org/node/752056

1
  • 1
    Thanks I will check this and let you know if that works. Commented Jul 9, 2016 at 15:17
0

may AJAX request is not send to server at all? Try changin the

$form['attributes']['submit'] = array(
'#type' => 'button',

to $form['attributes']['submit'] = array( '#type' => 'submit',

4
  • tried that but don't works. Commented Jul 4, 2016 at 17:04
  • if i undersand right: 1. create_report_form() works and gives you working button 2.reports_add_conditions() also works and gives working button. 3. reports_add_attribute_conditions() .... not working? Correct? Commented Jul 4, 2016 at 17:10
  • also i doubt that calling drupal_render() is good idea Commented Jul 4, 2016 at 17:19
  • @NabajitRoy see my suggestions answer below Commented Jul 4, 2016 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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