3

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).

2 Answers 2

0

I'm pretty sure this error will be coming from the following loop in taxonomy_autocomplete_validate()

 // Collect candidate vocabularies.
    $field = field_widget_field($element, $form_state);
    $vocabularies = array();
    foreach ($field['settings']['allowed_values'] as $tree) {
      if ($vocabulary = taxonomy_vocabulary_machine_name_load($tree['vocabulary'])) {
        $vocabularies[$vocabulary->vid] = $vocabulary;
      }
    }

which is collecting the vocabularies allowed for the field, check:

  1. Field is set to be a term reference
  2. Field has at least one allowed vocabulary set
  3. field_city is the correct machine name of the field in question
8
  • thank for your answer. Yes you are right line 1585 of the taxonomy module throws the error. 1. I think so otherwise fields would not be loaded? 2. Yes Amsterdam in this case? 3. This one I know for sure, the machine name is correct.
    – FLY
    Commented Apr 27, 2012 at 14:41
  • 1
    You could still create the form item without any of the above being true, but the auto complete wouldn't know what vocab to check
    – TCLopez
    Commented Apr 27, 2012 at 14:45
  • I've put in watchdog("fieldtest", print_r($field), array(), WATCHDOG_ALERT); on line 1584 of taxonomy.module resulting in a 1 printed in the log files out of print_r($field)
    – FLY
    Commented Apr 27, 2012 at 14:58
  • so It could be 1 or 2 as you suggested but how do I check that? I'm guessing setting the field to ` '#type' => 'taxonomy_term_reference',` in the form is not it ( done that and the field disappears )
    – FLY
    Commented Apr 27, 2012 at 15:25
  • 1
    oke so it was the machine name, the machine name is stad but if I change the 'taxonomy/autocomplete/field_city', to 'taxonomy/autocomplete/stad', or 'taxonomy/autocomplete/6', (vid) I still get the error but now as an alert box and none of the terms are shown in the auto tag widget textfield.
    – FLY
    Commented May 1, 2012 at 11:54
0

It works without validation and also by changing 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,
);

Also removing the #element_validate prevents the error from showing, so I guess I'll have to write a custom validation for it to really fix it.

Source: OP edit.

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.