I'm working on the donation module that is based on formAPI and I'm struggling with modifying some of the values based on different conditions. It seems like form_set_value() should handle it but it's not doing anything. I set up a multistep form so that a validation (and change of some of the values if needed) can take place before passing the input to an external URL for further processing.

See the part of the code that I'm concern with: it's at the bottom.

/** 
 * Define the form for entering a donation. 
 */ 
function donations_form($form_values = NULL) {
  $form['#multistep'] = TRUE;
	
  // Find out which step we are on. If $form_values is NULL, that means we are on step 1.
  $step = isset($form_values) ? (int) $form_values['step'] : 1;

  // Store next step in hidden field.
  $form['step'] = array(
    '#type' => 'hidden',
    '#value' => $step + 1
  );
  if ($step < 2) {
    $form['#redirect'] = FALSE;
    $button_name = t('Continue');
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => $button_name
  );
  
  switch($step) {
         case 1:
		  $form['doRecur'] = array(
		    '#type'  => 'hidden',
		    '#value' => 0,
		    '#name'  => 'doRecur',
		  );		
		  $form['recurStartNow'] = array(
		    '#type'  => 'hidden',
		    '#value' => TRUE,
		    '#name'  => 'recurStartNow',
		  ); 
		  break;

	  case 2:      
		  $form['recurStartNow'] = array(
		    '#type'  => 'hidden',
		    '#value' => $form_values['recurStartNow']
		  );  
	          $form['subtotal3Placeholder'] = array(
		    '#type'  => 'hidden',
		    '#value' => '0.00',    
		  );
		  $form['recurStartDatePlaceholder'] = array(
		    '#type'  => 'hidden',
		    '#name'  => 'recurStartDatePlaceholder',
		    '#value' => '',          
		  );        
		  $form['price3Placeholder'] = array(
		    '#type'  => 'value',
		    '#value' => '0.00',     
		  );
		  $form['quantity3Placeholder'] = array(
		    '#type'  => 'value',
		    '#value' => 0,      
		  );	
		  $form['price3'] = array(
                    '#type' => 'value',
		    '#value' => array(),    
                  );         
		  $form['subtotal3'] = array(
                    '#type' => 'value',
		    '#value' => array(),    
                  );         
		  $form['quantity3'] = array(
                    '#type' => 'value',
		    '#value' => array(),    
                  );         
		  $form['recurStartDate'] = array(
                    '#type' => 'value',
		    '#value' => array(),    
                  );         
      
		  $button_name = t('Proceed to secure page');		  
		  $urls = donations_urls();
		  $url = $urls[variable_get(PAYMENT_URL, PAYMENT_URL_TEST)];
		  $form['#action'] = $url;

		  $form['charge_total'] = array(
		    '#type'  => 'hidden',
		    '#value' => number_format($form_values['subtotal1'] + $form_values['subtotal2'], 2),
		    '#name'  => 'charge_total',
		  );      
		  $form['submit'] = array(
		    '#type'  => 'submit',
		    '#value' => $button_name,
		    '#name'  => 'submit',
		  );      
  }
  return $form;
}

/**
 * Validate the form.
 */
function donations_form_validate($form_id, $form_values, $form) {
	if (!valid_email_address($form_values['email'])) {
           form_set_error('email', t('Please enter a valid email address.'));
	}
	$decimal = (float)$form_values['subtotal2'];
        if (!is_numeric($form_values['subtotal2']) || $decimal != $form_values['subtotal2'] || $decimal <= 0) {
           form_set_error('subtotal2', t('The specified donation amount is invalid.'));
        }

        //check and setup the first payment
	$thisyear = date('Y');
	$thismonth = date('m');
	$thisday = date('d');
	$dday = 20; //billing day
	if ($dday <= $thisday) {
		//If today is past the dday (e.g. 20th) of current month, we will charge on the dday (e.g. 20th) following month
		$thismonth = $thismonth + 1;
	}
        $thedate = $thisyear . '/' . $thismonth . '/'.$dday; 
	form_set_value($form['recurStartDate'], $thedate);
 
        //change recurring payment parameters 
        if ($form_values['recurAmountPlaceholder'] > 0) {
		form_set_value($form['doRecur'], 1);
		form_set_value($form['price3'], $form_values['recurAmountPlaceholder']);
		form_set_value($form['quantity3'], $form_values['recurNum']);
	}
        $recur_subtotal = number_format($form_values['recurAmountPlaceholder'] * $form_values['recurnum'], 2);
        form_set_value($form['subtotal3'], $recur_subtotal);
}

I found some tips on the topic here: http://boldsource.com/form-api-tip-use-form-set-value-change-form-values... and some more here: http://drupal.org/node/160160#comment-279107, but they didn't solve the problem.

Thanks in advance for any help.

Comments

mkrakowiak’s picture

It looks like it doesn't work this way. It will not work if you use form_set_value() inside a validation function.

To make it work, I just broke it down to functions. So now it goes like this:

$form['quantity3'] = array(
   '#type'  => 'hidden',
   '#value' => changeQuantity3($form_id, $form_values, $form)    
);

and then the function

function changeQuantity3($form_id, $form_values, $form){
   $theQuantity = $form_values['recurNum'];
   return $theQuantity;
}