I have a project where I need to force the user to rate the content when they submit a comment. I have the fivestar voting widget configured to appear on the comment form using the form case in hook_comment. Now I'm trying to find a way to use the validate case in hook_comment to check if the user has submitted a rating. But the jquery.rating.js script doesn't affect the comment form in any way, so there is nothing that I can use form_set_error on to validate against. So I guess this is more of a jquery question than a fivestar question, but I'm looking for a way to use the $stars.click section of the javascript to fill in the value of a hidden form that will be part of the $a1 array when the comment is submitted. The hidden radio buttons that are filled by the $stars.click section of jquery.ratings.js do not show up in the $a1 array that is passed to the validate function in hook_comment, so i can't validate against them. At this point, this is my hook_comment implementation (I'll take care of hiding the checkbox through CSS or something) :

function commentrate_comment($a1, $op) {
  global $node;
  switch ($op) {
    case 'view':
    
        

      break;

    case 'form':
       
       $currentnode = $a1[nid]['#value'];
       $form['ratings'] = array(
         '#type' => 'fieldset',
         '#collapsible' => FALSE,
         '#weight' => 0,
         );
         
       $form['ratings']['fivestar_caption'] = array(
         '#value' => t('How do you feel about this article?'),
         '#prefix' => '<div class="fivestar-comment">',
         '#suffix' => '</div>',
         //'#weight' => 0,
         );
       $form['ratings']['fivestar_widget'] = array(
         '#value' => fivestar_widget_form($currentnode), // I modified Fivestar to accept the nid instead of the node array.
         //'#weight' => 0,
         );
       $form['ratings']['fivestar_voted'] = array(
         '#type' => 'checkbox',
         '#title' => t('validate'),
         '#prefix' => '<div class="fivestar-voted">',
         '#suffix' => '</div>',
         //'#weight' => 0,
         );
       return $form; 
    break; 

    case 'validate':
      //drupal_set_message(t('The contents of the a1 array are'. print_r($a1)));
      
    
     if ($a1[ratings][fivestar_voted] == 0){
        form_set_error('comment_form', t('You have to rate the content too.'));
        }
      
      
      break;

  }
} 

Comments

Anonymous’s picture

I found a way. I used this jQuery statement:

$("input[@type='checkbox']").each(function () { this.checked = true; }).end();

However, I have no idea what would happen if there were more than one checkbox on the page :-)

quicksketch’s picture

Status: Active » Closed (fixed)