0

I'm learning how to create a new module for the first time, so please bare with me. I'm trying to add a validation on the comment form from scratch; it's just not validating at all, meaning that my form successfully submits when I intentionally don't fill out the subject field.
Here is my code:

/*
 *  Implementation of hook_form_alter
 */
function my_module_form_alter(&$form, &$form_state, $form_id){
  if ($form_id == "comment_form") {
    $form['#validate'][] =  'my_module_comment_validate';
  }
}   

/**
  * Validate routine to prevent keywords from being used within comments.
  */
function my_module_comment_validate($form, &$form_state){

if ($form_state['values']['subject'] == '') {
 form_set_error('', t('Error!'));
 }
}
3
  • 2
    Have you reset the cache?
    – keithm
    Aug 9, 2011 at 23:05
  • Thanks for the reply. I have reset the cache and no luck.
    – gerl
    Aug 10, 2011 at 14:27
  • have you tried debugging by adding a dpm inside the validate function ? what is the output?
    – user2948
    Oct 27, 2011 at 9:09

2 Answers 2

1

In your form_set_error() call, you haven't set a specific field name on which to set an error. It's possible that without a form_set_error() call identifying a real form element no error is happening.

form_set_error('subject', t('Error!'));

Also, check to make sure your validation function is being called with a drupal_set_message()

0

change your form id...that should be

function MYMODULE_form_comment_form_alter(&$form, &$form_state, &$form_id){
}

and clear your cache..it should work for you

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.