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.


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, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.