Skip to main content
3 of 5
added 673 characters in body
FLY
  • 1.9k
  • 2
  • 26
  • 59

form with taxonomy autocomplete widget throws error upon submit: Invalid argument supplied for foreach()

I have a custom form with this field in it:

$form['first']['field_city'] = array(
    '#title' => t('Woonplaats'),
    '#type' => 'textfield',
    '#size' => 25,
    '#autocomplete_path' => 'taxonomy/autocomplete/field_city',
    '#element_validate' => array('taxonomy_autocomplete_validate'),
    '#required' => TRUE,
);

I'm trying to use the autocomplete tagging widget to use with an existing taxonomy vocabulary on this form. It works fine tags are loaded when I start typing but if I submit the form I get the following Warning:

Warning: Invalid argument supplied for foreach() in taxonomy_autocomplete_validate()

resulting in this error and the wsod (white screen of death)

   PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '))' at line 2: 
SELECT base.tid AS tid, base.vid AS vid, base.name AS name, base.description AS description, base.format AS format, base.weight AS weight, v.machine_name AS vocabulary_machine_name FROM {taxonomy_term_data} base INNER JOIN {taxonomy_vocabulary} v ON base.vid = v.vid WHERE (base.name LIKE :db_condition_placeholder_0 ESCAPE '\\') AND (base.vid IN ()) ; Array ( [:db_condition_placeholder_0] => Amsterdam )

I think I'm missing something in the form decleration or made a mistake somewhere. So what's wrong with it? Thanks!

some suggestion is that the machine_name of the field might be wrong. ( the machine name is actually stad when I look in the taxonomy in the admin UI )

While @TCLopez answer was helpful it still does not fix this problem please help!

Update: got it working without validation and changed the machine name of the taxonomy field using the UI from stad to field_city:

$voca = taxonomy_vocabulary_machine_name_load('field_city');
$form['first']['field_city'][$voca->vid] = array(
    '#type' => 'textfield',
    '#title' => $voca->name,
    '#required' => TRUE,
    '#field_name' => $voca->machine_name,
    // '#language' => 'nl',
    // '#field_parents' => array($voca->machine_name),
    // '#element_validate' => array('taxonomy_autocomplete_validate'),
    // '#default_value' => 'test_default_value',
    '#autocomplete_path' => 'taxonomy/autocomplete/' . $voca->machine_name,
    '#size' => 25,
);
FLY
  • 1.9k
  • 2
  • 26
  • 59