Search code examples
phpdrupaldrupal-7drupal-fapidrupal-forms

How can I display an error message if comment body text is not valid?


I am using MYMODULE_form_comment_form_alter to check comment's body text and I have a validation function:

function MYMODULE_form_comment_form_alter(&$form, &$form_state, &$form_id){
    $form['#validate'][] = 'MYMODULE_comment_form_validate';
}

function MYMODULE_comment_form_validate($form, &$form_state){
    $current_body = $form_state['values']['comment_body'][LANGUAGE_NONE][0]['value'];

    if (strpos($current_body, "www") == TRUE || strpos($current_body, "http") == TRUE) {
        form_set_error($form['comment_body'], "Error message");
    }
}

It is working perfectly but form still submitting values. How can do a function which doesn't submit if comment body value is not valid? Thanks!


Solution

  • Try using form_error() instead of form_set_error().

    Let me know whether it worked.

    Hope this works... Muhammad.