2

I have a checkbox with a link in its title text. This is a required field and the error message references the title including the link. How can I modify the error message to remove the link from the error message text?

hook_field_validate() appears not to be called; that was my first attempt after doing some searching.

2

5 Answers 5

3

Quick and easy fix is to use the String Overrides module. Here is a quote about it (from the module's project page):

Provides a quick and easy way to replace any text on the site.

Features:

  • Easily replace anything that's passed through t()
  • Locale support, allowing you to override strings in any language
  • Ability to import/export *.po files, for easy migration from the Locale module
  • Note that this is not a replacement to Locale as having thousands of overrides can cause more pain then benefit. Use this only if you need a few easy text changes.
2

You can try to override the strings or theme the message as has been mentioned already. Another option is to override the validation function for that form or that field, and raise the error yourself with the message you want.

You can use hook_form_alter in various ways. You could simply replace the #validate function on the submit button on the form, and thus provide your own validation function. In that function call form_set_error to raise the validation problems.

If you want to keep the core validation for most fields and just override the validation for one field you could set #limit_validation_errors to the submit button. This allows you to define which part of the form should have errors validated (and thus, which ones should not). You'll still want to add your own validation function for the form elements you've just ignored - so add it to the functions in the #validate of the submit button.

1

If a field is required then the validation check for an empty value is done inside Drupal's form handling and it'll never reach your custom code.

If you were to write a custom implementation of theme_form_element() in your theme or module you could add an extra data member say #display_title with your link in it and show that as the label text in the form if present. Forms validation would then use the regular #title value for messages.

1

If this is the only thing you want to change, you can override strings in your settings.php file. Check out the default.settings.php file for examples on how to do this. For only changing a string or two, this is better than adding the overhead of another module.

0

In Drupal 8, you can alter the messages in the hook_preprocess_HOOK. Example here: How to remove status messages from admin theme?

I am not sure whether you can do the same in Drupal 7, but you could apply the same logic in the theming layer, see: function theme_status_messages

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.