[development] Changing button #name?

Alex Bronstein alex at craftyspace.com
Wed Jun 23 16:21:07 UTC 2010

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.

More information about the development mailing list