I have the following form built with Form API. Its method is GET. I try to validate or submit, but it does not work. I have checked the method names, etc., and are correct, in fact I see an "attempt" of validation in some cases. I guess that there is something weird for the way I use the select autofilling field. It is a "car models" select whose values are filled by selection of "car brand" select, and I think that the code somehow interferes with the normal use of Form API.
function mymodule_search_form(){
$brands = get_brands();
$brand = get_default_brand();
$form['product'] = array(
'#type' => 'textfield',
'#title' => t('Product'),
'#autocomplete_path' => 'product-busq/productos/all',
'#attributes' => array("placeholder" => t('Product'), ) // 'class' => array('auto_submit')
);
$form['brand'] = array(
'#type' => 'select',
'#title' => t('Brand'),
'#options' => $brands,
'#default_value' => $brand,
'#ajax' => array(
'callback' => 'mymodule_models_update',
'wrapper' => 'models_wrapper',
'progress' => array(
'message' => '',
'type' => 'throbber',
),
),
);
$form['model'] = array(
'#type' => "select",
'#title' => t('Model'),
'#required' => FALSE,
'#multiple' => YES,
'#attributes' => array("placeholder" => t('Model'), ), // 'class' => array('auto_submit')
'#prefix' => '<div id="models_wrapper">',
'#suffix' => '</div>',
'#disabled' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#name' => ""
);
$form["#action"] = $base_url . "/" . $language->language . "/" . drupal_get_path_alias("node/3");
$form["#method"] = "get";
return $form;
} // function
And now the autofilling function which may be causing the problems, especially the last sentence. The code is copied from a tutorial somewhere:
function mymodule_models_update($form, &$form_state) {
unset($form_state['input']['model'], $form_state['values']['model']);
$brand = $form_state['values']['brand'];
$brand_desc = "";
$results = db_query("SELECT description FROM {taxonomy_term_data} tax WHERE tax.tid = :tid", array(":tid" => $brand));
foreach($results as $row) $brand_desc = $row->description;
if($brand_desc != "") $form['model']['#options'] = _hook_list_models($brand_desc);
unset($form['model']["#attributes"]['disabled']);
$form['model']['#disabled'] = FALSE;
$form["model"]["#attributes"]["id"] = "edit-model";
return form_builder($form['#id'], $form['model'], $form_state);
} // function
hook_
prefix. It is reserved for implementing hook API.$form_state['rebuild'] = TRUE;
instead of callingform_builder()
to rebuild the form. In the ajax callback, you should only return the dynamic part only. In your case, it isreturn $form['model'];