Permalink
Browse files

Only require one required radio input in set to be checked.

  • Loading branch information...
1 parent 8b147fb commit 688ffdceef5308396b9555492b0f3667ec3507d5 @JangoSteve JangoSteve committed Sep 6, 2012
Showing with 29 additions and 2 deletions.
  1. +9 −2 src/rails.js
  2. +20 −0 test/public/test/call-remote-callbacks.js
View
@@ -252,12 +252,19 @@
// Helper function which checks for blank inputs in a form that match the specified CSS selector
blankInputs: function(form, specifiedSelector, nonBlank) {
var inputs = $(), input, valueToCheck,
- selector = specifiedSelector || 'input,textarea';
- form.find(selector).each(function() {
+ selector = specifiedSelector || 'input,textarea',
+ $allInputs = form.find(selector);
+ $allInputs.each(function() {
input = $(this);
valueToCheck = input.is(':checkbox,:radio') ? input.is(':checked') : input.val();
// If nonBlank and valueToCheck are both truthy, or nonBlank and valueToCheck are both falsey
if (!valueToCheck === !nonBlank) {
+
+ // Don't count unchecked required radio if other radio with same name is checked
+ if (input.is(':radio') && $allInputs.filter('input:radio:checked[name="' + input.attr('name') + '"]').length) {
+ return true; // Skip to next input
+ }
+
inputs = inputs.add(input);
}
});
@@ -234,6 +234,26 @@ asyncTest('unchecked required radio should abort form submission', 1, function()
}, 13);
});
+asyncTest('required radio should only require one to be checked', 1, function() {
+ var form = $('form[data-remote]')
+ .append($('<input type="radio" name="yes_no" required="required" value=1 id="checkme">'))
+ .append($('<input type="radio" name="yes_no" required="required" value=2>'))
+ .removeAttr('data-remote')
+ .bind('iframe:loading', function() {
+ ok(true, 'form should get submitted');
+ })
+ .bind('ujs:everythingStopped', function() {
+ ok(false, 'ujs:everythingStopped should not run');
+ })
+ .find('#checkme').prop('checked', true)
+ .end()
+ .trigger('submit');
+
+ setTimeout(function() {
+ start();
+ }, 13);
+});
+
function skipIt() {
// This test cannot work due to the security feature in browsers which makes the value
// attribute of file input fields readonly, so it cannot be set with default value.

0 comments on commit 688ffdc

Please sign in to comment.