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:
Currently, I can only get:
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.
正确答案
| 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)));
}
| 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);
}
| answered Jul 14 '14 at 10:12
|
| | |
| Are you passing the input element name to form_set_error() ?
According to function name you can pass one message per function call.
| | answered Aug 2 '11 at 9:16
|
| | |
| 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.
| | answered Sep 6 '11 at 14:19
|
| | |
|
来自 http://drupal.stackexchange.com/questions/8327/how-to-set-multiple-error-messages-with-form-set-error