0

I have a weird question. Let's say we have this form:

$form['field1'] = array(
  '#type' => 'textfield',
  '#title'  => 'First field'
);
$form['container'] = array(
  '#type' => 'container'
);
$form['container']['field2'] = array(
  '#type' => 'textfield',
  '#title' => '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 :)

2
  • Just use the '#element_validate' => array('function_that_validates_your_field'), in the form element creation to send that form item to a specific validation. maybe have a read through the field api part of the examples module. – tecjam May 3 '12 at 13:27
  • It wouldn't help. I don't need to validate anything, I just need to know how I can find the exact position of that field's definition array inside the form array. – Marius Ilie May 3 '12 at 13:43
0

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(&$elements) {
  foreach ($elements as $type => $value) {
    $elements[$type]['#pre_render'][] = 'YOUR_MODULE_element_error_check';
  }
}

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

I ended up adapting the general solution to each individual form I needed to achieve this functionality by looking at the form structure and setting the suffix that way.

0

I have no idea how Drupal knows how to fetch the correct form element just from the name without indicating the parents.

As for getting the form element from the name that form_get_errors() gives you, the following worked for me:

$name = 'container][field2';

// Split the elements/indexes apart.
$split_name = explode('][', $name);

// Check for integer indexes. (Not sure if this is entirely necessary.
foreach($split_name as &$arr_nest) {
  // Don't quote integer indexes.
  $arr_nest = !is_int($arr_nest) ? '"' . $arr_nest . '"' : $arr_nest;
}

// Put the name back together again. eg. "container"]["field2" 
$split_name = implode('][', $split_name);

// Get the whole element from the form.
eval('$element = $form[' . $split_name . '];');

// Check the results
dsm($element, 'form element');
0

Maybe you could check the Inline Form Errors module. I think it will suit your needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.