Hello,
I have created a small module,where i tried to execute a small code using ahah method. I have called a validate function on submitting the form. While submitting the form second select list is showing in a different location. I have attached screenshot here. Can anyone please help me out to solve this issue?
function ahah_test_menu() {
$items['ahah_test'] = array(
'title' => 'AHAH Test',
'page callback' => 'drupal_get_form',
'page arguments' => array('ahah_test_form'),
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM
);
$items['ahah_test/js'] = array(
'title'=>'Who cares',
'page callback'=>'ahah_test_something',
'access callback'=>TRUE,
'type'=>MENU_CALLBACK
);
}
function ahah_test_form($form_state) {
$form['field'] = array(
'#type'=>'fieldset',
'#title'=>'field',
'#tree' => TRUE,
);
$form['field']['country'] = array(
'#type'=>'select',
'#title'=>'Country',
'#options'=>array('Ireland','United States'),
'#ahah' => array(
'path'=> 'ahah_test/js',
'wrapper'=>'place_1',
'event'=>'change',
'method'=>'replace',
),
);
$form['field']['wrapper'] = array(
'#type'=>'item',
'#prefix'=>'
'#suffix'=>'
',
);
$form['b_button'] = array(
'#type'=>'submit',
'#value'=>'button b',
'#submit'=>array('b_submit'),
'#validate'=>array('ahah_test_validate'),
);
return $form;
}
function ahah_test_validate($form,&$form_state){
form_set_error('country','please select');
return false;
}
function ahah_test_something() {
$form = array(
'#type'=>'checkboxes',
'#value'=>'button b',
'#options'=>array('Ireland','United States'),
);
$output = cotation_render($form, 'selected_institutions');
print drupal_to_js(array('data' => $output, 'status' => true));
exit();
}
function cotation_render($fields, $name) {
$form_state = array('submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];
$form = form_get_cache($form_build_id, $form_state);
$form[$name] = $fields;
form_set_cache($form_build_id, $form, $form_state);
$form += array(
'#post' => $_POST,
'#programmed' => FALSE,
);
// Rebuild the form.
$form = form_builder($_POST['form_id'], $form, $form_state);
// Render the new output.
$new_form = $form[$name];
return drupal_render($new_form);
}