I am trying to fine-tune the validation of a rather complex webform that makes use of nested fieldsets, but I cannot have the non validated fields be highlighted as errors (i.e. with red borders).

My form has a textfield named "cartoncini" that is contained in two nested fieldsets (called "materiale_richiesto" and "dettaglio_richiesta"). The value inserted in that field must be a number, so I setup the following validation rule:

<?PHP
$nCartoncini = $form_values['submitted_tree']['materiale_richiesto']['dettaglio_richiesta']['cartoncini'];
if (strlen($nCartoncini) > 0) {
  if (!eregi("^[0-9]{1,}$", $nCartoncini)) {
    form_set_error('submitted[materiale_richiesto][dettaglio_richiesta][cartoncini', t('Il numero di cartoncini ordinati non &egrave; valido.'));
  }
}
?>

The rule is working, in the sense that the user gets the error message if the value he puts in the field is not a number, but the textfield is not visually marked with a red border.

I checked the $form element #parent of the field (as suggested in Drupal API documentation for form_set_error http://api.drupal.org/api/function/form_set_error) and it is as expected:

[#parents] => Array
  (
    [0] => submitted
    [1] => materiale_richiesto
    [2] => dettaglio_richiesta
    [3] => cartoncini
  )

Please note that when I use a similar code for a textfield that is used within a single fieldset element all is working fine. For example, this validation snippet both raise an error and highligts the "email" field:

<?PHP
$email_address = $form_values['submitted_tree']['dati_anagrafici']['email'];
if (strlen(trim($email_address)) > 0) {
  if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email_address))
    form_set_error('submitted][dati_anagrafici][email', t('L\'indirizzo di posta ' . $email_address . ' non &egrave; valido.'));
};
?>

Comments

quicksketch’s picture

Category: bug » support
Status: Active » Closed (fixed)

Your line:

    form_set_error('submitted[materiale_richiesto][dettaglio_richiesta][cartoncini', t('Il numero di cartoncini ordinati non &egrave; valido.'));

Should be:

    form_set_error('submitted][materiale_richiesto][dettaglio_richiesta][cartoncini', t('Il numero di cartoncini ordinati non &egrave; valido.'));

However support for custom code is not provided in the Webform issue queue, so I'm closing this issue directly.