7 replies [Last post]
z3b
z3b's picture
Offline
Joined: 08/15/2007
Juice: 51
Was this information Helpful?

I've a couple of CCK field attached to the "product" content type.
Maybe I'm wrong but it seems 'validate' is never called on product nodes.
I can hook 'submit' without problem but no luck with 'validate'.
Works fine with other content types.
Any idea ?
Thanks!
(UC-Beta3)

z3b
z3b's picture
Offline
Joined: 08/15/2007
Juice: 51
Re: hook_nodeapi('validate')

bump

Lyle's picture
Offline
AdministratoreLiTe!
Joined: 08/07/2007
Juice: 6846
Re: Re: hook_nodeapi('validate')

I think at some point hook_validate() wasn't working like I thought it should, so I added in a validate handler to the form. But because I wanted to see if it was a Drupal problem or an Übercart problem, I changed it back. Now it works fine.

If you've got some form_alter that's overwriting the validate handlers, that will prevent the product validation from working. This would be fixed with the latest revision, 845.

z3b
z3b's picture
Offline
Joined: 08/15/2007
Juice: 51
Re: Re: Re: hook_nodeapi('validate')

Thanks Lyle, works fine now !
Cheers,

glennnz's picture
Offline
Joined: 01/20/2009
Juice: 451
Lyle I don't understand your

Lyle

I don't understand your response here, sorry. Still learning to write modules. I'm trying to get a validate to work too. I've used hook_form_alter to add some checkboxes to a product node that I want to be required.

I can't get the validate to work.

My code is:

<?php
// $Id: course_pre_requirements.module,v 0.1

/*******************************************************************************
* Hook Functions
******************************************************************************/
/**
* Implementation of hook_form_alter().
*/

function course_pre_requirements_form_alter($form_id, &$form) {
 
$form_nid = $form['nid']['#value'];
  switch (
$form_id) {
      case
'uc_product_add_to_cart_form_'.$form_nid:
   
$form['course_pre_requirements_fieldset'] = array(
     
'#weight' => '-2',
     
'#type' => 'fieldset',
     
'#title' => t('Course Pre-entry Requirements'),
    );
   
$form['course_pre_requirements_fieldset']['description'] = array(
     
'#type' => 'markup',
     
'#value' => '<strong>You must confirm that you have fulfilled all of the course pre-entry requirements by checking the boxes below before you can register on this course.</strong>',
    );
   
$form['course_pre_requirements_fieldset']['requirement1'] = array(
     
'#type' => 'checkbox',
     
'#title' => 'I have......',
     
'#required' => 'TRUE',
    );
   
$form['course_pre_requirements_fieldset']['requirement2'] = array(
     
'#type' => 'checkbox',
     
'#title' => 'I have......',
     
'#required' => 'TRUE',
    );
   
$form['course_pre_requirements_fieldset']['requirement3'] = array(
     
'#type' => 'checkbox',
     
'#title' => 'I have......',
     
'#required' => 'TRUE',
    );
   
$form['course_pre_requirements_fieldset']['requirement4'] = array(
     
'#type' => 'checkbox',
     
'#title' => 'I have......',
     
'#required' => 'TRUE',
    );
    break;
  }
}

function

course_pre_requirements_form_validate($form_id, $form_values) {
 
$error_tag_large_start = '<h3 class="form-required">';
 
$error_tag_large_end = '</h3>';
 
$error_tag_start = '<h1 class="form-required"><li>';
 
$error_tag_end = '</li></h1>';
 
$errors_found = false;
 
$output = '';
 
$output .= $error_tag_large_start . 'Errors were found, please review the following items and resubmit this form:' . $error_tag_large_end . '<ul>';

  if(

$form_values['requirment1'] == 0) {
     
$output .= $error_tag_start . 'Requirement 1 not set' . $error_tag_end;
     
$errors_found = true;
  }
  if(
$form_values['requirement2'] == 0) {
     
$output .= $error_tag_start . 'Requirement 2 not set' $error_tag_end;
     
$errors_found = true;
  }
  if(
$form_values['requirement3'] == 0) {
     
$output .= $error_tag_start . 'Requirement 3 not set' $error_tag_end;
     
$errors_found = true;
  }
  if(
$form_values['requirement4'] == 0) {
     
$output .= $error_tag_start . 'Requirement 4 not set' $error_tag_end;
     
$errors_found = true;
  }

 

$output .= '</ul>';

  if(

$errors_found == true) {
   
drupal_set_message($output);
  }

}

?>

The other thing I need to achieve with this is restrict the hook_form_alter to products of a particular class. How would I do this?

Thanks

Glenn

Lyle's picture
Offline
AdministratoreLiTe!
Joined: 08/07/2007
Juice: 6846
Re: Lyle

Saw what the problem is. You don't need a validate handler just to make fields required, since Drupal's Form API handles that for you.

#required should be the boolean value TRUE, not the string 'TRUE'.

<?php
    $form
['course_pre_requirements_fieldset']['requirement4'] = array(
     
'#type' => 'checkbox',
     
'#title' => 'I have......',
     
'#required' => TRUE,
    );
?>
glennnz's picture
Offline
Joined: 01/20/2009
Juice: 451
Lyle Nope, can't get this to

Lyle

Nope, can't get this to work. I've changed all my '#required' values to boolean TRUE, but it makes no difference, the product just gets added to the cart anyway.

Also, I need to restrict this hook_form_alter to products of a particular class, how can I do that?

Thanks

Glenn

glennnz's picture
Offline
Joined: 01/20/2009
Juice: 451
I've tried this too, with no

I've tried this too, with no success.

<?php
function course_pre_requirements_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch (
$op) {
    case
'validate':
 
$error_tag_large_start = '<h3 class="form-required">';
 
$error_tag_large_end = '</h3>';
 
$error_tag_start = '<h1 class="form-required"><li>';
 
$error_tag_end = '</li></h1>';
 
$errors_found = false;
 
$output = '';
 
$output .= $error_tag_large_start . 'Errors were found, please review the following items and resubmit this form:' . $error_tag_large_end . '<ul>';

  if(

$form_values['requirment1'] == 0) {
     
$output .= $error_tag_start . 'Requirement 1 not set' . $error_tag_end;
     
$errors_found = true;
  }
  if(
$form_values['requirement2'] == 0) {
     
$output .= $error_tag_start . 'Requirement 2 not set' $error_tag_end;
     
$errors_found = true;
  }
  if(
$form_values['requirement3'] == 0) {
     
$output .= $error_tag_start . 'Requirement 3 not set' $error_tag_end;
     
$errors_found = true;
  }
  if(
$form_values['requirement4'] == 0) {
     
$output .= $error_tag_start . 'Requirement 4 not set' $error_tag_end;
     
$errors_found = true;
  }

 

$output .= '</ul>';

  if(

$errors_found == true) {
   
drupal_set_message($output);
  }
  break;
  }
}
?>