欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

drupal form_set_error multiple fields How to set multiple error messages with form_set_error?

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.

This is standard Drupal behaviuor – drupality 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


正确答案 

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)));
}







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

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 = array();
$errors[] = 'My element error';
$errors[] = 'Another error for the same element';

if ($errors) {
  form_set_error_multiple('my_element', $errors);
}
shareimprove this answer
 

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

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

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

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.

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




来自  http://drupal.stackexchange.com/questions/8327/how-to-set-multiple-error-messages-with-form-set-error


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

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 = array();
$errors[] = 'My element error';
$errors[] = 'Another error for the same element';

if ($errors) {
  form_set_error_multiple('my_element', $errors);
}
shareimprove this answer
 

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

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

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

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.

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

普通分类: