Validation does not work when both fields being validated are in the same fieldset.

I have two email field keys: 'email' and 'email2'. When both are contained in a fieldset, the validation (code below) does not work. When one field is taken out or both fields are taken out of the fieldset, the validation works fine.

<?php
  if ($form_values['submitted_tree']['email'] != $form_values['submitted_tree']['email2']) {
    form_set_error('submitted][email', t('E-mail addresses must match.'));
  }
?>

I assumed the fieldset would make no difference, but apparently it does. I'm not an advanced Drupal user, so I'm not sure what the best workaround is for this.

Comments

zbricoleur’s picture

Similar issue here for a single text field. While the field is in the fieldset, if the input is invalidated by the code, the error message gets posted, but the field does not get highlighted. If I move the field out of the fieldset, it does get highlighted.

EDIT: Fixed it. I just didn't have the correct name of the form element.

Makes me wonder, though, if this could be the problem for the other poster as well. In my case I had to use 'submitted][fieldset_name][fieldname' to get it to work properly.

quicksketch’s picture

Category: bug » support
Status: Needs work » Closed (fixed)

Yep, for nested elements you need to go all the way down the tree like zbricoleur noted in #1.