I am sure that I do not understand something fundamental about Drupal's form API but I hope that someone can help me with that:
I want to create some kind of custom CAPTCHA as the CAPTCHA module states that with caching enabled on your project problems could appear and my project really needs caching via the Boost module. The idea was the following in my forms building array:
$number1 = rand(0, 10);
$number2 = rand(0, 10);
$form['numbercheck'] = array(
'#type' => 'numberfield',
'#title' => 'Please fill in the sum of '. $number1 .' and '. $number2,
'#required' => TRUE,
);
$form_state['storage']['checksum'] = $number1 + $number2;
Then in validation I wanted to do the following check:
if($form_state['storage']['checksum'] != $form_state['values']['numbercheck']) {
form_set_error('numbercheck', 'The sum was not correct');
}
But every time I fill in the form the following happens: say the first random number is 1 and the second number is 2 - I fill in the correct value "3" but get an error "The sum was not correct". Using Devel module and dpm()-function to get the values of $form_state['storage']['checksum'] and the single random numbers I realize that this value changed after submitting the form - while viewing the form the value is correctly "3" but when validation starts the value change to let's say 4 because the then newly created numbers in the then newly created form are maybe 1 and 3.
I tried
$form_state['rebuild'] = FALSE;
in form building function as I thought that the form may be rebuild before validation starts and therefore the random numbers were newly created as well as the $form_state array may be changed but this did not solve my problem.
I hope that somebody can help me out with that - thank you very much in advance!
Best, Tobias