0

I am using a custom validation in my Drupal 7 comment form.
I want to match the comment_body against a word "example" so if someone just type "example"in the comment body then it will show a error message that only the word "example" is not allowed. After reading drupal api documentation i codded like this:

function mango_form_comment_node_news_form_alter(&$form, &$form_state, $form_id){
    $form['comment_body']['#prefix'] = '<div class="sm">Post your comment now!</div>';
    $form['actions']['submit']['#value'] = 'Post Your Comment';
    $form['#validate'][] = 'banana';
}

function banana($form, &$form_state) {
  if ($form_state['comment_body'] == "example") {
    form_set_error('comment_body', 'Your comment can not contain example!');
  }
}

Do not working :'( what i'm missing? Thanks

  • 1
    Have a read of this, and then inspect the $form to find the exact element you should be referring to (probably something like $form_state['values']['comment_body'][LANGUAGE_NONE][0]['value']) – Clive Jan 7 '14 at 13:09
  • Thank you @Clive your answer on that post really help me to understand the array's. – rakibtg Jan 7 '14 at 14:46
0

The values for comment_body is not stored in $form_state but in something like:

$form_state['values']['comment_body']['und'][0]['value']

function banana($form, &$form_state) {
  if ($form_state['values']['comment_body']['und'][0]['value'] == "example") {
    form_set_error('comment_body', 'Your comment can not contain example!');
  }
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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