Changing button #name?
Hi! I'm trying to build an AHAH form which allows users to add and remove steps. I'm building a table like:
Id Note ---------------------------------------------- 1 [This is a step ] <remove> 2 [This is another step ] <remove> 3 [Make me an offer! ] <remove>
<add step>
In order to be able to determine which remove button was pressed, I'm creating them like: array( '#type' => 'button', '#name' => 'remove_btn_' . $row_id, '#value' => 'remove', '#ahah' => ... ) I'm doing this because otherwise the form state shows the clicked_button always as the last of the remove buttons created. According to the forms_api_reference on api.drupal.org, in the section for '#name',
All button and submit elements on a form should have the same name
Am I breaking something by setting the name for each button separately? If so, how am I supposed to distinguish between the various remove buttons? Thanks, Ricky The information in this e-mail is intended only for the person to whom it is addressed. If you believe this e-mail was sent to you in error and the e-mail contains patient information, please contact the Partners Compliance HelpLine at http://www.partners.org/complianceline . If the e-mail was sent to you in error but does not contain patient information, please contact the sender and properly dispose of the e-mail.
Does it have to be a giant form? Using anchor tags and then a confirm form is fairly common. Objects in a form do need to have unique names / IDs. - Ken Winters On Jun 23, 2010, at 10:36 AM, Richard Morse wrote:
Hi! I'm trying to build an AHAH form which allows users to add and remove steps.
I'm building a table like:
Id Note ---------------------------------------------- 1 [This is a step ] <remove> 2 [This is another step ] <remove> 3 [Make me an offer! ] <remove>
<add step>
In order to be able to determine which remove button was pressed, I'm creating them like:
array( '#type' => 'button', '#name' => 'remove_btn_' . $row_id, '#value' => 'remove', '#ahah' => ... )
I'm doing this because otherwise the form state shows the clicked_button always as the last of the remove buttons created.
According to the forms_api_reference on api.drupal.org, in the section for '#name',
All button and submit elements on a form should have the same name
Am I breaking something by setting the name for each button separately? If so, how am I supposed to distinguish between the various remove buttons?
Thanks, Ricky
The information in this e-mail is intended only for the person to whom it is addressed. If you believe this e-mail was sent to you in error and the e-mail contains patient information, please contact the Partners Compliance HelpLine at http://www.partners.org/complianceline . If the e-mail was sent to you in error but does not contain patient information, please contact the sender and properly dispose of the e-mail.
Actually all buttons in a Drupal form have the name "op" so that you can easily determine which button was pressed in the submit function. i.e. if ($op = 'blah) or switch ($op). If you are using the Drupal Ahah framework for this then your submit function should handle it. But since all your buttons are titled Remove, what you can do is use $form_state['clicked_button']['#id'] to determine which button was clicked in your submit function. As #id is built of the array key you give the element in the $form variable. ----- Adam A. Gregory Drupal Developer & Consultant Web: AdamAGregory.com Twitter: twitter.com/adamgregory Phone: 910.808.1717 Cell: 919.306.6138 On Wed, Jun 23, 2010 at 11:15 AM, Ken Winters <kwinters@coalmarch.com>wrote:
Does it have to be a giant form? Using anchor tags and then a confirm form is fairly common.
Objects in a form do need to have unique names / IDs.
- Ken Winters
On Jun 23, 2010, at 10:36 AM, Richard Morse wrote:
Hi! I'm trying to build an AHAH form which allows users to add and remove
steps.
I'm building a table like:
Id Note
---------------------------------------------- 1 [This is a step ] <remove> 2 [This is another step ] <remove> 3 [Make me an offer! ] <remove>
<add step>
In order to be able to determine which remove button was pressed, I'm creating them like:
array( '#type' => 'button', '#name' => 'remove_btn_' . $row_id, '#value' => 'remove', '#ahah' => ... )
I'm doing this because otherwise the form state shows the clicked_button always as the last of the remove buttons created.
According to the forms_api_reference on api.drupal.org, in the section for '#name',
All button and submit elements on a form should have the same name
Am I breaking something by setting the name for each button separately? If so, how am I supposed to distinguish between the various remove buttons?
Thanks, Ricky
The information in this e-mail is intended only for the person to whom it is addressed. If you believe this e-mail was sent to you in error and the e-mail contains patient information, please contact the Partners Compliance HelpLine at http://www.partners.org/complianceline . If the e-mail was sent to you in error but does not contain patient information, please contact the sender and properly dispose of the e-mail.
On Jun 23, 2010, at 11:37 AM, Adam Gregory wrote:
Actually all buttons in a Drupal form have the name "op" so that you can easily determine which button was pressed in the submit function. i.e. if ($op = 'blah) or switch ($op).
If you are using the Drupal Ahah framework for this then your submit function should handle it. But since all your buttons are titled Remove, what you can do is use $form_state['clicked_button']['#id'] to determine which button was clicked in your submit function. As #id is built of the array key you give the element in the $form variable.
I know that by default they are all named 'op'. This is my problem; I cannot distinguish the buttons by their '#value', because it is always the same. I'm solving this by setting the '#name' to allow me to determine precisely which button was clicked. If I don't set '#name', then *regardless* of which button I press, the 'clicked_button' structure in the $form_state corresponds to the last button of the same '#value' added to the form. Part of the awkwardness here is that HTML doesn't allow you to display some text in a button while having a different value. Ricky The information in this e-mail is intended only for the person to whom it is addressed. If you believe this e-mail was sent to you in error and the e-mail contains patient information, please contact the Partners Compliance HelpLine at http://www.partners.org/complianceline . If the e-mail was sent to you in error but does not contain patient information, please contact the sender and properly dispose of the e-mail.
I think the line of documentation in the FAPI reference that says all buttons must have the same #name should be updated. They all have #name='op' by default. It is perfectly legitimate to have any particular button use a different #name, and for different buttons on the same form to have different #name. For $form_state['clicked_button'] to be set correctly, all buttons must have a unique #name/#value combination, so if they need the same #value, then different #name is perfectly acceptable. At least, that's my opinion. Perhaps someone on this list might know why the FAPI reference says otherwise. Richard Morse wrote:
On Jun 23, 2010, at 11:37 AM, Adam Gregory wrote:
Actually all buttons in a Drupal form have the name "op" so that you can easily determine which button was pressed in the submit function. i.e. if ($op = 'blah) or switch ($op).
If you are using the Drupal Ahah framework for this then your submit function should handle it. But since all your buttons are titled Remove, what you can do is use $form_state['clicked_button']['#id'] to determine which button was clicked in your submit function. As #id is built of the array key you give the element in the $form variable.
I know that by default they are all named 'op'. This is my problem; I cannot distinguish the buttons by their '#value', because it is always the same. I'm solving this by setting the '#name' to allow me to determine precisely which button was clicked. If I don't set '#name', then *regardless* of which button I press, the 'clicked_button' structure in the $form_state corresponds to the last button of the same '#value' added to the form. Part of the awkwardness here is that HTML doesn't allow you to display some text in a button while having a different value.
Ricky
The information in this e-mail is intended only for the person to whom it is addressed. If you believe this e-mail was sent to you in error and the e-mail contains patient information, please contact the Partners Compliance HelpLine at http://www.partners.org/complianceline . If the e-mail was sent to you in error but does not contain patient information, please contact the sender and properly dispose of the e-mail.
Hi Richard Are you using '#tree' => TRUE? If so, you can access $form_state['clicked_button']['parents'] which will be an array of the keys of the parent elements. If each row in your table is built as follows: $form['rows'][$row_id]['field1'] = array(...), $form['rows'][$row_id]['field2'] = array(...) etc and you are rendering this as a table using a theme function and element_children to determine the valid values of $row_id (is there any other way?) then your ['clicked_button']['parents'] will contain an array including the final op but also the 'parent' $row_id, this can tell you which button was pressed. Have a look at the quicktabs module for an example. HTH Lee Rowlands -----Original Message----- From: development-bounces@drupal.org [mailto:development-bounces@drupal.org] On Behalf Of Richard Morse Sent: Thursday, 24 June 2010 2:03 AM To: development@drupal.org Subject: Re: [development] Changing button #name? On Jun 23, 2010, at 11:37 AM, Adam Gregory wrote:
Actually all buttons in a Drupal form have the name "op" so that you can easily determine which button was pressed in the submit function. i.e. if ($op = 'blah) or switch ($op).
If you are using the Drupal Ahah framework for this then your submit function should handle it. But since all your buttons are titled Remove, what you can do is use $form_state['clicked_button']['#id'] to determine which button was clicked in your submit function. As #id is built of the array key you give the element in the $form variable.
I know that by default they are all named 'op'. This is my problem; I cannot distinguish the buttons by their '#value', because it is always the same. I'm solving this by setting the '#name' to allow me to determine precisely which button was clicked. If I don't set '#name', then *regardless* of which button I press, the 'clicked_button' structure in the $form_state corresponds to the last button of the same '#value' added to the form. Part of the awkwardness here is that HTML doesn't allow you to display some text in a button while having a different value. Ricky The information in this e-mail is intended only for the person to whom it is addressed. If you believe this e-mail was sent to you in error and the e-mail contains patient information, please contact the Partners Compliance HelpLine at http://www.partners.org/complianceline . If the e-mail was sent to you in error but does not contain patient information, please contact the sender and properly dispose of the e-mail. Internal Virus Database is out of date. Checked by AVG - www.avg.com Version: 8.5.406 / Virus Database: 271.1.1/2686 - Release Date: 02/13/10 19:35:00
Hi Lee, You're correct that $form_state['clicked_button'], if it is set correctly, contains lots of useful information, including #id, #parents, and #array_parents. The problem is anything based on $form_state['clicked_button'] requires that it be set to the button that was actually clicked, and that can only happen when no two buttons within a form contain identical #name and #value. If buttons have the same #name and different #value, all is ok. If buttons have different #name and the same #value, all is ok. But if two buttons have the same #name and the same #value, there is no way for the Form API or any server-side code to know which of the buttons was actually clicked, since clicking either button would send the exact same $_POST information. Alex. Lee Rowlands wrote:
Hi Richard Are you using '#tree' => TRUE? If so, you can access $form_state['clicked_button']['parents'] which will be an array of the keys of the parent elements. If each row in your table is built as follows: $form['rows'][$row_id]['field1'] = array(...), $form['rows'][$row_id]['field2'] = array(...) etc and you are rendering this as a table using a theme function and element_children to determine the valid values of $row_id (is there any other way?) then your ['clicked_button']['parents'] will contain an array including the final op but also the 'parent' $row_id, this can tell you which button was pressed. Have a look at the quicktabs module for an example. HTH Lee Rowlands
-----Original Message----- From: development-bounces@drupal.org [mailto:development-bounces@drupal.org] On Behalf Of Richard Morse Sent: Thursday, 24 June 2010 2:03 AM To: development@drupal.org Subject: Re: [development] Changing button #name?
On Jun 23, 2010, at 11:37 AM, Adam Gregory wrote:
Actually all buttons in a Drupal form have the name "op" so that you can easily determine which button was pressed in the submit function. i.e. if ($op = 'blah) or switch ($op).
If you are using the Drupal Ahah framework for this then your submit function should handle it. But since all your buttons are titled Remove, what you can do is use $form_state['clicked_button']['#id'] to determine which button was clicked in your submit function. As #id is built of the array key you give the element in the $form variable.
I know that by default they are all named 'op'. This is my problem; I cannot distinguish the buttons by their '#value', because it is always the same. I'm solving this by setting the '#name' to allow me to determine precisely which button was clicked. If I don't set '#name', then *regardless* of which button I press, the 'clicked_button' structure in the $form_state corresponds to the last button of the same '#value' added to the form. Part of the awkwardness here is that HTML doesn't allow you to display some text in a button while having a different value.
Ricky
The information in this e-mail is intended only for the person to whom it is addressed. If you believe this e-mail was sent to you in error and the e-mail contains patient information, please contact the Partners Compliance HelpLine at http://www.partners.org/complianceline . If the e-mail was sent to you in error but does not contain patient information, please contact the sender and properly dispose of the e-mail.
Internal Virus Database is out of date. Checked by AVG - www.avg.com Version: 8.5.406 / Virus Database: 271.1.1/2686 - Release Date: 02/13/10 19:35:00
Yes, sorry should have said this was in conjunction with previous comments as well :-) -----Original Message----- From: development-bounces@drupal.org [mailto:development-bounces@drupal.org] On Behalf Of Alex Bronstein Sent: Thursday, 24 June 2010 10:52 AM To: development@drupal.org Subject: Re: [development] Changing button #name? Hi Lee, You're correct that $form_state['clicked_button'], if it is set correctly, contains lots of useful information, including #id, #parents, and #array_parents. The problem is anything based on $form_state['clicked_button'] requires that it be set to the button that was actually clicked, and that can only happen when no two buttons within a form contain identical #name and #value. If buttons have the same #name and different #value, all is ok. If buttons have different #name and the same #value, all is ok. But if two buttons have the same #name and the same #value, there is no way for the Form API or any server-side code to know which of the buttons was actually clicked, since clicking either button would send the exact same $_POST information. Alex. Lee Rowlands wrote:
Hi Richard Are you using '#tree' => TRUE? If so, you can access $form_state['clicked_button']['parents'] which will be an array of the keys of the parent elements. If each row in your table is built as follows: $form['rows'][$row_id]['field1'] = array(...), $form['rows'][$row_id]['field2'] = array(...) etc and you are rendering this as a table using a theme function and element_children to determine the valid values of $row_id (is there any other way?) then your ['clicked_button']['parents'] will contain an array including the final op but also the 'parent' $row_id, this can tell you which button was pressed. Have a look at the quicktabs module for an example. HTH Lee Rowlands
-----Original Message----- From: development-bounces@drupal.org [mailto:development-bounces@drupal.org] On Behalf Of Richard Morse Sent: Thursday, 24 June 2010 2:03 AM To: development@drupal.org Subject: Re: [development] Changing button #name?
On Jun 23, 2010, at 11:37 AM, Adam Gregory wrote:
Actually all buttons in a Drupal form have the name "op" so that you can easily determine which button was pressed in the submit function. i.e. if ($op = 'blah) or switch ($op).
If you are using the Drupal Ahah framework for this then your submit function should handle it. But since all your buttons are titled Remove, what you can do is use $form_state['clicked_button']['#id'] to determine which button was clicked in your submit function. As #id is built of the array key you give the element in the $form variable.
I know that by default they are all named 'op'. This is my problem; I cannot distinguish the buttons by their '#value', because it is always the same. I'm solving this by setting the '#name' to allow me to determine precisely which button was clicked. If I don't set '#name', then *regardless* of which button I press, the 'clicked_button' structure in the $form_state corresponds to the last button of the same '#value' added to the form. Part of the awkwardness here is that HTML doesn't allow you to display some text in a button while having a different value.
Ricky
The information in this e-mail is intended only for the person to whom it is addressed. If you believe this e-mail was sent to you in error and the e-mail contains patient information, please contact the Partners Compliance HelpLine at http://www.partners.org/complianceline . If the e-mail was sent to you in error but does not contain patient information, please contact the sender and properly dispose of the e-mail.
Internal Virus Database is out of date. Checked by AVG - www.avg.com Version: 8.5.406 / Virus Database: 271.1.1/2686 - Release Date: 02/13/10 19:35:00
Internal Virus Database is out of date. Checked by AVG - www.avg.com Version: 8.5.406 / Virus Database: 271.1.1/2686 - Release Date: 02/13/10 19:35:00
participants (5)
-
Adam Gregory -
Alex Bronstein -
Ken Winters -
Lee Rowlands -
Richard Morse