I am using FormsAPI to create a wizard. One of the buttons is "Start Again" which resets a step number to 0. I have the step number as a hidden field on the form, and then use drupal_set_value to alter the value and then return back to the form.

Here's the code: -

This is the part in the form creation code: -

	if (!isset($form_values)) {
		$step = 1;
	} else {
		$step = $form_values['step'] + 1;
	}
	  
	$form['step'] = array(
		'#type' => 'hidden',
		'#value' => $step,
	);

This is the code in the validation part (which I know for sure is executed, including the part within the IF block): -

if ($form_state['clicked_button']['#name'] == 'reset') {
		form_set_value($form['step'], 0, $form_state);
		return $form;
	}

No matter what, the "Step" value does not go back to 0....

Thanks,

Dubs

Comments

nevets’s picture

Try changing the type from 'hidden' to 'value'

jaypan’s picture

Seconded.

$form['step'] = array(
'#type' => 'value',
'#value' => $step,
)

Contact me to contract me for D7 -> D10/11 migrations.

dubs’s picture

Thanks for your answers, but unfortunately it didn't work for me.

The first two lines of my Form code is: -

function repayment_calculator_calculate_new_form($form_state, $node) {
	
	kprint_r($form_state);

Step is now missing from the output, and therefore cannot be checked.

What else am I missing?

Thanks,

Dubs

dubs’s picture

Just for reference, I've realised I've been taking the wrong approach.

FormsAPI has a storage area which is perfect for me.

Using $form_state['storage']['step'] = 0; allows me to pass values between the form, validation and submit functions...