It's not hook_form_alter()
that can change the error message, since there isn't any error message when that hook is invoked. The error message is output from the form validation handler, which calls form_error(), or form_set_error() to set the error for the field that doesn't pass the validation.
If you want to change the error message for the specific form, and not globally, you can:
- Add a new form validation handler that is executed after the default one.
- In the validation handler, look for the error string in
$_SESSION['messages']
, and replace it with the string you want to show.
$_SESSION['messages']
is the variable where drupal_set_message() store all the messages it receives. drupal_set_message()
is the function called by form_set_error()
, which is the function called by form_error()
. The minimal code the validation handler should execute is similar to the following one.
if (isset($_SESSION['messages']['error'])) {
$messages = array_flip($_SESSION['messages']['error']);
$message = t('The machine-readable name is already in use. It must be unique.');
if (isset($messages[$message])) {
unset($messages[$message]);
$messages = array_flip($messages);
$messages[] = t('your message error');
$_SESSION['messages']['error'] = $messages;
}
}
There is still a place where the error message is stored, and it is the static variable returned from form_get_errors()
. It doesn't seem there is any module that looks for a specific value in that array; if there is any module doing that, for compatibility reasons is better not to change the error message reported in that array.
If the message error is used in a single form, you want to change that error message in any form, or there isn't any reason the error message is dynamically altered, then the easier way to change the error message is to:
- Use the String Overrides module as Clive suggest.
Add the following code to settings.php.
$conf['locale_custom_strings_en'][''] = array(
'The machine-readable name is already in use. It must be unique.' => 'Your error message.',
);
In the latter case, the string can be altered for any language; the following code changes the string for Italian.
$conf['locale_custom_strings_it'][''] = array(
'The machine-readable name is already in use. It must be unique.' => 'Your error message, in Italian.',
);
Between the two solutions, the first is preferable because it doesn't require you to alter the settings.php file; the second is preferable because it doesn't require any module, and it is preferable if the strings to alter are not too much, and you don't have many languages enabled in the site.