Form API - match field with error I have a weird question. Let's say we have this form:

$form['field1'] = array(
  '#type' =&gt; 'textfield',
  '#title'  =&gt; 'First field'
);
$form['container'] = array(
  '#type' =&gt; 'container'
);
$form['container']['field2'] = array(
  '#type' =&gt; 'textfield',
  '#title' =&gt; 'Second field'
);


Now if you set an error for field2, you do it like this:

form_set_error('field2', 'error message');


and not like 

form_set_error('container][field2', 'error message');


I wonder how does drupal does recognize the right form element to add the error class. I wanna do something like that, to add a suffix for every field who did not passed validation.

I tried getting the error field with form_get_errors() but it returns field2 as a field with an error. It's obvious that $form['field2'] does not exists add I can't add a suffix to it.

Any help is greatly appreciated. Or at least a hint to the right direction. Thanks :)
Drupal does it on elements theme level with _form_set_class function. This function gets $element array and checks if it has errors with function form_get_error. Because errors are set with indexes like 'foo][bar][value' (parents) that can be not so easy to find them in the $form array. So I suggest to use the same check as in _form_set_class with your function at pre_render, theme or validate level when you have $element array. hook_element_info_alter can be useful.  

The super simple example:

function YOUR_MODULE_element_info_alter(&amp;$elements) {
  foreach ($elements as $type =&gt; $value) {
    $elements[$type]['#pre_render'][] = 'YOUR_MODULE_element_error_check';
  }
}

function YOUR_MODULE_element_error_check($element) {
  if (isset($element['#parents']) &amp;&amp; form_get_error($element)) {
    $element['#suffix'] = t('error!');
  }
  return $element;
}