Let's suppose I have a content type Person, and I don't want the field Name to contain the text AAA.

What would be the best way of avoid submitting the form if that happens?

For example:
- Hiding the Save button
- Submitting it, but return to the same page with that field marked as red, or just show a message...

This is just an example. I'm asking it because sometimes I need to do a query to the database and see if some records exist, or something like that...

Edit: I have edited the template.php file under the theme folder I'm using, and I have added the functions:

  • theme_theme()
  • theme_formId($form)

In the 2nd function, I've modified the form to see Drupal is calling this function, and yes, it works...

Then I did the following in the 2nd function:

$form['#validate'] = array('my_validate_function');

and under my_validate_function($form, &$form_state) I just wrote form_set_error('', t('Testing...')); and nothing seems to be happening: I can still create a new content without problems :(

What am I missing here?

2 Answers 2


You can add validation callbacks to any form in Drupal using the Form API. Specifically, you can use hook_form_alter to add a validation callback to the $form['#validate'] element.

  • let me ask you something. Where should I declare that callback? in my template.php? Mar 26, 2011 at 6:28
  • The best practice here would be to declare this code in a custom module. Putting this code in your theme won't work in Drupal 6 (and even in 7, custom validators should be placed in a custom module rather than in a theme).
    – jhedstrom
    Mar 26, 2011 at 20:31
  • I will read how to create custom modules, and I'll give it a try. Will let people now how it was... Mar 27, 2011 at 0:31
  • It worked perfect! Mar 30, 2011 at 2:07

Add a custom validation handler, and define your criteria.

  • Could you be more specific about this? I'll edit my question to say what I've tried so far. Mar 26, 2011 at 7:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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