In a form_validate hook, I have some custom validation and I'm using

form_set_error('field_foo', "error_message");

to show validation errors. If the validation returns an error that is not linked to a particular form field, how do I show the error, indipendently? (For example: the error is you compiled more that three times the form.)

EDIT: I though to use:

drupal_set_message("error_message", 'error');

From the documentaion for 7.x

When a validation error is detected, the validator calls form_set_error() to indicate which element needs to be changed and provide an error message.

So, form_set_error() produce an error linked to a particular field of the form. I needed to display error indipendently in a custom complex validation, being errors not really about a field. I though to use form_set_error() on a random field, but it is not the perfect solution, for the field was highlighted and only the last error produced this way was visible. drupal_set_message solves this two problem, but the validation success and the form continues like there was no error.

2 Answers 2


The way core Drupal does this is to use '' for the first parameter of form_set_error(). So in the example you give, form_set_error('', "error_message"); would set the error message for the form, not tied to any specific form element.

This is an undocumented feature of form_set_error(), but it's a feature that core Drupal has used forever, over many different versions of Drupal core (including the current Drupal 9 version!). There is currently an issue open in the core Drupal issue queue asking for this feature to be documented. See https://www.drupal.org/project/drupal/issues/2780209


There is a plain solution


Does not highlight any field, and the validation fails as expected. It doesn't allow multiple message, but this can be solved using a little hack

$error_messages = "";
if (type_error_1)
   $error_messages .= "Error Message on First Line <br/>";
if (type_error_2){
   $error_messages .= "Error Message on Another Line <br/>";
if(strlen($error_messages) > 0){

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.