form_api: same form called multiple times, wrong form_state values
Hi all, I have a small admin form (2 buttons and a hidden value) that is repeated multiple times (each time the value is different). Now whenever I access $form_state['values'] from the submit function, the hidden value always returns the data from the first form. When I print out $_POST directly I do get the correct data. I've been searching this and it seems this is because the form_state is cached. I couldn't find any workaround though, so for the moment working with $_POST. Anyone know the correct way to do this ? Tnx, HnLn
There are two problems that I know of with multiple forms on a page in D6: http://drupal.org/node/386678 (fixed in D7): If any form uses $form_state['storage'] then it destroys $_POST, causing the other(s) not to be able to submit. This one has a demo module that you may be able to use to see if you're having the same problem. http://drupal.org/node/384992 (still open in D7): In an AJAX/AHAH form, form_clean_id() (D6) or drupal_html_id (D7) doesn't work right, so you end up having to use hook_forms() to make the form_id of each form different. -Randy On Sat, Mar 13, 2010 at 6:53 AM, Hans Langouche <hans.langouche@gmail.com>wrote:
Hi all,
I have a small admin form (2 buttons and a hidden value) that is repeated multiple times (each time the value is different).
Now whenever I access $form_state['values'] from the submit function, the hidden value always returns the data from the first form. When I print out $_POST directly I do get the correct data.
I've been searching this and it seems this is because the form_state is cached. I couldn't find any workaround though, so for the moment working with $_POST. Anyone know the correct way to do this ?
Tnx,
HnLn
-- Randy Fay Drupal Development, troubleshooting, and debugging randy@randyfay.com +1 970.462.7450
When you get an answer, please let me know, as I keep score on these :-) Thanks, -Randy On Sat, Mar 13, 2010 at 12:35 PM, Hans Langouche <hans.langouche@gmail.com>wrote:
Hi Randy,
tnx for the help, but neither of those (which I came across as well) are relevant in this case. 'storage' isn't used and neither is ajax.
Greetz,
HnLn
Randy Fay wrote:
There are two problems that I know of with multiple forms on a page in D6:
http://drupal.org/node/386678 (fixed in D7): If any form uses $form_state['storage'] then it destroys $_POST, causing the other(s) not to be able to submit. This one has a demo module that you may be able to use to see if you're having the same problem.
http://drupal.org/node/384992 (still open in D7): In an AJAX/AHAH form, form_clean_id() (D6) or drupal_html_id (D7) doesn't work right, so you end up having to use hook_forms() to make the form_id of each form different.
-Randy
On Sat, Mar 13, 2010 at 6:53 AM, Hans Langouche <hans.langouche@gmail.com>wrote:
Hi all,
I have a small admin form (2 buttons and a hidden value) that is repeated multiple times (each time the value is different).
Now whenever I access $form_state['values'] from the submit function, the hidden value always returns the data from the first form. When I print out $_POST directly I do get the correct data.
I've been searching this and it seems this is because the form_state is cached. I couldn't find any workaround though, so for the moment working with $_POST. Anyone know the correct way to do this ?
Tnx,
HnLn
-- Randy Fay Drupal Development, troubleshooting, and debugging randy@randyfay.com +1 970.462.7450
-- Randy Fay Drupal Development, troubleshooting, and debugging randy@randyfay.com +1 970.462.7450
In Drupal, I recommend staying away from hidden values as they actually do allow for a miniscule possibility of hacking. Instead use '#type' => 'value' which will not present on the rendered form. I have never had any trouble with altering it in multistep forms. Nancy E. Wichmann, PMP Injustice anywhere is a threat to justice everywhere. -- Dr. Martin L. King, Jr. ________________________________ From: Hans Langouche <hans.langouche@gmail.com> To: development@drupal.org Sent: Sat, March 13, 2010 8:53:26 AM Subject: [development] form_api: same form called multiple times, wrong form_state values Hi all, I have a small admin form (2 buttons and a hidden value) that is repeated multiple times (each time the value is different). Now whenever I access $form_state['values'] from the submit function, the hidden value always returns the data from the first form. When I print out $_POST directly I do get the correct data. I've been searching this and it seems this is because the form_state is cached. I couldn't find any workaround though, so for the moment working with $_POST. Anyone know the correct way to do this ? Tnx, HnLn
Hi Hans When you inspect the form using firebug/web developer toolbar - what form id/form build id does each form have? If they are all the same then you've got a problem and you might need to use hook_forms. This is how (for example) Ubercart adds an 'add to cart' button for multiple products on one page (ie multiple forms per page). These 'add to cart' forms sound similar to yours - ie a button. Perhaps if you have a look at how uc_product_forms works you might be able to do something similar (http://api.lullabot.com/uc_product_forms) Lee On Sat, 13 Mar 2010 14:53:26 +0100, Hans Langouche
I have a small admin form (2 buttons and a hidden value) that is repeated multiple times (each time the value is different).
Now whenever I access $form_state['values'] from the submit function, the hidden value always returns the data from the first form. When I print out $_POST directly I do get the correct data.
I've been searching this and it seems this is because the form_state is cached. I couldn't find any workaround though, so for the moment working with $_POST. Anyone know the correct way to do this ?
On Sat, Mar 13, 2010 at 6:53 AM, Hans Langouche <hans.langouche@gmail.com> wrote:
I have a small admin form (2 buttons and a hidden value) that is repeated multiple times (each time the value is different).
Now whenever I access $form_state['values'] from the submit function, the hidden value always returns the data from the first form. When I print out $_POST directly I do get the correct data.
I've been searching this and it seems this is because the form_state is cached. I couldn't find any workaround though, so for the moment working with $_POST. Anyone know the correct way to do this ?
I ran into this a while ago myself. You have to do a little trickery with the form_id value so that each form on the page has a unique ID. If the forms are for users then maybe the UID will work. If they are for node teasers maybe the node ID. Then you create a hook_forms value that gets called and resets the callback to be a specific general function. See http://api.drupal.org/api/function/hook_forms and perhaps some of the modules which use it like fivestar.module and notifications_ui.module (I think that one does...one of the notifications/messaging modules does). Please ask if you need more advice - it's a tricky problem to resolve that cost me more time than I'd like to admit. Regards, Greg -- Greg Knaddison | 303-800-5623 | http://growingventuresolutions.com Mastering Drupal - http://www.masteringdrupal.com
Amazon Store has to do this: function amazon_store_forms($form_id) { $args = func_get_args(); $forms = array(); if (strpos($form_id, 'amazon_store_addcart_form') === 0) { $forms[$form_id] = array( 'callback' => 'amazon_store_addcart_form', 'callback arguments' => $args[1], ); } if (strpos($form_id, '_amazon_store_cart_quantity_form') === 0) { $forms[$form_id] = array( 'callback' => '_amazon_store_cart_quantity_form', 'callback arguments' => $args[1], ); } return $forms; } Then instead of just drupal_get_form('form_id'), you have to do drupal_get_form('form_id_plus_key'), if I remember right. -Randy On Sun, Mar 14, 2010 at 4:46 PM, Greg Knaddison < Greg@growingventuresolutions.com> wrote:
On Sat, Mar 13, 2010 at 6:53 AM, Hans Langouche <hans.langouche@gmail.com> wrote:
I have a small admin form (2 buttons and a hidden value) that is repeated multiple times (each time the value is different).
Now whenever I access $form_state['values'] from the submit function, the hidden value always returns the data from the first form. When I print out $_POST directly I do get the correct data.
I've been searching this and it seems this is because the form_state is cached. I couldn't find any workaround though, so for the moment working with $_POST. Anyone know the correct way to do this ?
I ran into this a while ago myself. You have to do a little trickery with the form_id value so that each form on the page has a unique ID. If the forms are for users then maybe the UID will work. If they are for node teasers maybe the node ID. Then you create a hook_forms value that gets called and resets the callback to be a specific general function.
See http://api.drupal.org/api/function/hook_forms and perhaps some of the modules which use it like fivestar.module and notifications_ui.module (I think that one does...one of the notifications/messaging modules does).
Please ask if you need more advice - it's a tricky problem to resolve that cost me more time than I'd like to admit.
Regards, Greg
-- Greg Knaddison | 303-800-5623 | http://growingventuresolutions.com Mastering Drupal - http://www.masteringdrupal.com
-- Randy Fay Drupal Development, troubleshooting, and debugging randy@randyfay.com +1 970.462.7450
participants (5)
-
Greg Knaddison -
Hans Langouche -
Lee Rowlands -
nan wich -
Randy Fay