I'm sure this is something incredibly simple I'm doing wrong, but I've been staring at this too long.
I have a custom Drupal 7 module that builds a form and handles the submission/validation of that form.
There is a value that is set when the form is built, which I am storing in a hidden field. When the form is submitted, in the validation stage I need to get the latest value from the database and compare it to the value that was submitted in the form to make sure it hasn't changed since the form was built.
What seems to be happening, however, is that the value of this field is getting recalculated after the form is submitted, so by the time it gets to the validation stage it always matches what is in the database regardless of how it started. It's not an ajax form.
Here is a much simplified version of the code:
function my_menu() {
$items['playlist/show'] = array(
'access callback' => 'user_access',
'access arguments' => array('blah'),
'title' => '',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_form', arg(2)),
'type' => MENU_CALLBACK,
);
return $items
}
function my_form($form, &$form_state, $type) {
$my_value = get_value(); //my value is 10
$form['playing-now-item'] = array('#type' => 'hidden', '#value' => $my_value);
}
function my_form_validate($form, &$form_state) {
$latest_value = get_value(); //latest value is 11
if ($form_state['values']['playing-now-item'] != $latest_value) {
//do stuff here
}
}
In that example - $form_state['values']['playing-now-item'] would be 11 when I get to the validate stage.
Any ideas?
Comments
hi dude,Generally On form
hi dude,
Generally On form submit the function call in the order (1.my_form, 2.my_form_validate, 3.my_form_submit)...
Now change your code as follows...
Rgds.,
hilarudeens
Ah, I never noticed that the
Ah, I never noticed that the form function is called again on submit - I guess I've never run into this use case before.
So the code above presents the same problem - $form_state['values'] still seems to be empty during the run of the form function after submit.
So if I do:
When I first load the form, I get:
1
82
If I then change the database value to 83, then submit the form, I get:
1
83 (from after submit)
1
83 (from form loading again)
Thanks for pointing me in the right direction though, will keep plugging away at this.
So this is what ultimately
So this is what ultimately worked in D7. Thanks hilarudeens for pointing me in the right direction.