0

I have a form which presents three input fields which are later combined into a single phone number. When that phone number doesn't validate (doesn't match /^\d{10}$/), I want to show a single message saying the phone number is incorrectly formatted, but I want to highlight all three input fields. Is this possible? The best I have come up with is this:

form_set_error('firstBox', $theMessage);
form_set_error('secondBox', " ");
form_set_error('thirdBox', " ");

This way, all fields are highlighted and I get the message only once, but I still get bullet points for the empty messages...

enter image description here

Is there a better solution for this?

2
  • Are these in a fieldset? If so, you should be able to set the form error on that (eg, the parent) and it should cascade down through to the individual fields with just one error message. drupal.org/node/678816 talks to this briefly.
    – Jimajamma
    Sep 17, 2012 at 15:59
  • They are in a fieldset, but they are not alone. Indeed, the phoneNumber fieldset also has a field for the extension and another one for the category (residential, work, etc). I suppose I could add a fieldset in there to solve my problem though, but I'll wait to see if there's a better solution first.
    – Shawn
    Sep 17, 2012 at 16:10

1 Answer 1

1

D'oh. In Drupal 6 at least, just call form_set_error(FIELD) without the message and it should highlight the field and not set a message. You are sending a space, so you are getting a space as a message.

Code in Question:

function form_set_error($name = NULL, $message = '', $reset = FALSE) {
  static $form = array();
  if ($reset) {
    $form = array();
  }
  if (isset($name) && !isset($form[$name])) {
    $form[$name] = $message;
    if ($message) {
      drupal_set_message($message, 'error');
    }
  }
  return $form;
}  
4
  • a quick look at the code in Drupal 7 suggests the same behavior, just call form_set_error() with only the first argument.
    – Jimajamma
    Sep 17, 2012 at 16:13
  • I just tried that and the fields are not highlighted at all if I call form_set_error with the first argument only.
    – Shawn
    Sep 17, 2012 at 16:16
  • Grr... Sorry :(
    – Jimajamma
    Sep 17, 2012 at 16:20
  • Well, you could go this route: api.drupal.org/api/drupal/includes%21form.inc/function/… and clean up messages and errors manually.
    – Jimajamma
    Sep 17, 2012 at 16:33

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.