1

I've set up my review system of my Drupal 7.x site some what like mentioned in this http://drupal.org/node/1308114 and I've also set that field as "Required" but it seems that "Required Field" is not working since, its allowing user to submit the review without rating the article, for that I've created my custom module containing

<?php
/**
 * Implementation of hook_form_alter()
 */
function rishihrvalidate_form_alter(&$form, $form_state, $form_id) {
    $form['#attributes']['onsubmit'] = 'return comment_cs_validate()';
    if ($form_id == 'comment-form'){
  } 
}

calling the JavaScript

<script type="text/javascript">
      function comment_cs_validate() {
        if ($('#edit-field-review-value div div').hasClass("on") && $('#edit-field-review-atmosphere div div').hasClass("on") && $('#edit-field-review-service div div').hasClass("on")) {
          return true;
        }
        else {
          alert ('You must rate the listing before submitting your review');
          return false;
        }
      }
    </script>

Though the above PHP code was not perfect, it was executing correctly and behaving as desired only the problem I'm getting is while logging on my site also its taking place since I've called all the submit buttons, hence I changed the from that above one to this, but now I'm getting nothing, may I know where I might be lacking?

<?php
/**
 * Implementation of hook_form_alter()
 */
function rishihrvalidate_form_alter(&$form, &$form_state, $form_id) {
    if ($form_id == 'comment_form') {
        $form['#attributes']['onsubmit'] = 'return comment_cs_validate()';
    }
}

I'm not getting where is the mistake, can someone help me in this issue?

Thanks in Advance :)

1 Answer 1

2

Perhaps you are using the wrong $form_id

One way to check is to use Database Logging, also known as the Watchdog or Recent Log Entries, to log the $form_id. After you add this code, execute the form by performing a submit, and then check your Reports > Recent Log Entries area for the result. This may require enabling the Database Logging core module before the messages are logged and/or visible.

<?php
/**
 * Implementation of hook_form_alter()
 */
function rishihrvalidate_form_alter(&$form, &$form_state, $form_id) {
  // Log the $form_id to the watchdog
  // in this example, 'form_id' is just the log entry name. I commonly use 'wtf'
  watchdog('form_id', $form_id);

  if ($form_id == 'comment_form') {
    $form['#attributes']['onsubmit'] = 'return comment_cs_validate()';
  }
}
?>

For more information about watchdog, visit http://api.drupal.org/api/function/watchdog

2
  • Thanks for your help, it worked; using the above method I came to know that the problem was with my $form_id i.e. my form-id was wrong/incorrect I changed the code to this and it worked! Thanks for your help :) Commented Dec 5, 2011 at 9:34
  • Above is the newly updated code :) I was getting the error cause of the incorrect $form_id which was 'comment_node_hospital_form' and I was using 'comment_form'. Commented Dec 15, 2011 at 8:55

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.