7

I notice I can pass in a message variable to form_set_error().

How can I pass in multiple messages?

I tried calling form_set_error() several times, but always the first one is displayed.

So, to sum up I want to show something like this:

  • error 1
  • error 2
  • error 3

Currently, I can only get:

  • error 1

Then, once the users correct it, and resubmit it, they get:

  • error 2

    if (strpos($form_state["values"]["body"], "

    if (strpos($form_state["values"]["body"], "

If both of these control statements trigger an error, only the first one is shown. After the user corrects the error, the second error message is shown.

3
  • This is standard Drupal behaviuor
    – Codium
    Aug 2 '11 at 11:32
  • But when I call form_set_error I do see multiple messages in D6. Infact I'd like to ask how to make FAPI show only the very first message passed in form_set_error.
    – AgA
    Dec 26 '11 at 18:04
  • AgA he's talking about only a single error message per form element, there can be multiple per form.
    – leexonline
    May 26 '14 at 20:29
10

I would change the code to issue a single call to form_set_error() per element:

$errors = array();
if (strpos($form_state["values"]["body"], "<h1") === true)
    $error_messages["body"][] = "Blalalabalbal bablalbabla.";

if (strpos($form_state["values"]["body"], "<h3") === false)
    $error_messages["body"][] = "Do this do that balbablalba.";

foreach ($error_messages as $element => $messages) {
    form_set_error($element, theme('item_list', array('items' => messages)));
}
2
  • Thanks Janus, I thought of that too. But I really like the bullet points form_set_error generates...I guess I will have to do what you mention though.
    – giorgio79
    Aug 3 '11 at 13:10
  • 1
    giorgio, theme('item_list') generates a bullet point list (an ul, or unordered list)
    – zerolab
    Sep 6 '11 at 23:07
3

I've created separate function for that:

function form_set_error_multiple($name = NULL, $messages = '', $limit_validation_errors = NULL) {
  return form_set_error('extra][items', implode('</li><li>', $messages));
}

And then I can call it like that:

$errors = [
  'My element error',
  'Another error for the same element',
]

form_set_error_multiple('my_element', $errors);
1

Are you passing the input element name to form_set_error()?

According to function name you can pass one message per function call.

1
  • Yes, passing input element. Also provided the code. Any help is appreciated.
    – giorgio79
    Aug 2 '11 at 10:08
0

I solved this with a "hack":

$error_messages = "";
if (strpos($form_state["values"]["body"], "<h1") === true)
   $error_messages .= "Error Message on First Line <br/>";
}
if (strpos($form_state["values"]["body"], "<h3") === false){
   $error_messages .= "Error Message on Another Line <br/>";
}
// next check if there are any errors, and if so, write them to form_set_error
if(strlen($error_messages) > 0){
  form_set_error('',$error_messages);
}

This will display all accumulated errors as Drupal message.

1
  • 1
    strpos does not return true, pl. correct your second line.
    – AgA
    Dec 26 '11 at 18:03

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.