Skip to main content

Search and Top Navigation

#8761 closed bug (fixed)

Opened November 01, 2012 01:23AM UTC

Closed January 28, 2014 03:58PM UTC

Last modified January 28, 2014 03:59PM UTC

Button: Non-button elements with the same name as a radio cause an exception

Reported by: reubenhelms Owned by:
Priority: minor Milestone: 1.11.0
Component: ui.button Version: 1.9.1
Keywords: Cc:
Blocked by: Blocking:
Description

My jsFiddle example is here http://jsfiddle.net/reubenhelms/ZtZu8/

This shows a radio set in a div that is formatted by buttonset() on a div.

It also shows a hidden field that shares the same name as the radio set, but is outside the div targeted by the radio set.

Hidden fields often accompany a radio or checkbox field to send a value to the server, to denote the field exists, but nothing was selected.

When selecting an option in the radio set, a console error is generated "Uncaught Error: cannot call methods on button prior to initialization; attempted to call method 'widget'.

This error occurs because radioGroup() picks up all elements in a form that share the same name as the selected radio button, and then assumes that all items have associated buttons.

Restricting elements to the same type as the selected radio button, or restricting elements to the div of the original buttonset target would prevent this error, and allow the usage of hidden fields to denote an unselected buttonset.

Attachments (0)
Change History (13)

Changed November 01, 2012 01:41AM UTC by scottgonzalez comment:1

resolution: → wontfix
status: newclosed

I've seen people add hidden fields to accompany checkboxes, but they usually have a different name and then there's som server side logic that detects the hidden field being submitted and knows that the checkbox should be handled even if it's not submitted. However, I've never seen someone use the same name before. I don't think this is something that the widget should handle as it's quite confusing to mix types while using the same name.

Changed November 01, 2012 02:00AM UTC by reubenhelms comment:2

That's a little disappointing. My own example comes from CakePHP, except that I've modified it slightly to move the hidden field outside of the div.

How many frameworks that generate radio inputs with an accompanying input field in that manner would it take, before considering that it is a standard practice to use the same name?

Changed November 01, 2012 02:05AM UTC by scottgonzalez comment:3

I honestly can't give you an exact number, but I can tell you that button has been in stable jQuery UI releases for 2.5 years and this is the first time someone has reported this. Perhaps you can ask CakePHP why they use the same name or see if someone from their dev team is willing to make a strong argument in support of this on this ticket.

Changed December 23, 2012 05:42PM UTC by kristoffer comment:4

I would say that this way to validate a radio set worked very well with jQuery UI 1.8. With that said, I would consider this as a bug. Try to change the jQuery UI version from 1.9.1 to 1.8.24 in the jsFiddle posted in the bug report and you will see that the buttonset works as expected.

Changed January 28, 2014 03:12AM UTC by phazeii comment:5

I believe Yii framework does it as well. I've seen it in quite a number of places myself. Just came across this bug and would be good to see it fixed.

It's more often seen with checkboxes ala:

<input type="hidden" name="mybox" value="0">
<input type="checkbox" name="mybox" value="1">

So the value is submitted if the box is unchecked.

Also seen as popular solution here : http://stackoverflow.com/questions/1809494/post-the-checkboxes-that-are-unchecked

And it does actually seem to work just fine with a hidden if it's with checkboxes:

http://jsfiddle.net/ZtZu8/40/

So should it not have the same functionality with radio's as well?

Changed January 28, 2014 03:13AM UTC by phazeii comment:6

_comment0: I believe Yii framework does it as well. I've seen it in quite a number of places myself. Just came across this bug and would be good to see it fixed. \ \ It's more often seen with checkboxes ala: \ \ {{{ \ <input type="hidden" name="mybox" value="0"> \ <input type="checkbox" name="mybox" value="1"> \ }}} \ \ So the value is submitted if the box is unchecked. \ Also seen as popular solution here : http://stackoverflow.com/questions/1809494/post-the-checkboxes-that-are-unchecked \ \ And it does actually seem to work just fine with a hidden if it's with checkboxes: \ http://jsfiddle.net/ZtZu8/40/ \ \ \ So should it not have the same functionality with radio's as well? \ \ 1390878844341571

Sorry about the double submit, it said there was a captcha error the first time.

Changed January 28, 2014 01:34PM UTC by tj.vantoll comment:7

scott.gonzalez: Are you ok with moving

radioGroup
into a method on the prototype? I think this is an edge case, but right now if you're using a framework that does this you're screwed.

Changed January 28, 2014 01:56PM UTC by tj.vantoll comment:8

With

_radioGroup()
, this extension solves the problem:

$.widget( "ui.button", $.ui.button, {
	_radioGroup: function( radio ) {
		return this._super( radio ).filter(function() {
			return $( this ).parents( ".ui-buttonset" ).length > 0;
		});
	}
});

Changed January 28, 2014 02:25PM UTC by scottgonzalez comment:9

Well, I definitely don't want to document it as a supported method. Let's see what the performance hit is for including [type="radio"] in the selector.

Changed January 28, 2014 02:47PM UTC by tj.vantoll comment:10

milestone: 1.10.0none
resolution: wontfix
status: closedreopened
summary: click of a button in a buttonset console errors on elements not a part of the button setButton: console errors on elements not a part of the button set

Changed January 28, 2014 03:08PM UTC by scottgonzalez comment:11

status: reopenedopen
summary: Button: console errors on elements not a part of the button setButton: Non-button elements with the same name as a radio cause an exception

Ok, let's just add that check.

Changed January 28, 2014 03:58PM UTC by TJ VanToll comment:12

resolution: → fixed
status: openclosed

Button: Ignore non-radio elements with the same name

Fixes #8761

Closes gh-1185

Changeset: ccb13240dd8b5cfac0199a30dcec4a71cbe1b252

Changed January 28, 2014 03:59PM UTC by tj.vantoll comment:13

milestone: none1.11.0