Permalink
Browse files

Use :checked for required checkbox and radio inputs. Fixes #236.

  • Loading branch information...
1 parent 43bfec1 commit 1e184328c69b76cd7c1bb66cacc3439f737b56ab @JangoSteve JangoSteve committed Aug 14, 2012
Showing with 33 additions and 3 deletions.
  1. +4 −3 src/rails.js
  2. +29 −0 test/public/test/call-remote-callbacks.js
View
@@ -246,12 +246,13 @@
// Helper function which checks for blank inputs in a form that match the specified CSS selector
blankInputs: function(form, specifiedSelector, nonBlank) {
- var inputs = $(), input,
+ var inputs = $(), input, valueToCheck,
selector = specifiedSelector || 'input,textarea';
form.find(selector).each(function() {
input = $(this);
- // Collect non-blank inputs if nonBlank option is true, otherwise, collect blank inputs
- if (nonBlank ? input.val() : !input.val()) {
+ 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) {
inputs = inputs.add(input);
}
});
@@ -191,6 +191,35 @@ asyncTest('blank required form input for non-remote form with "novalidate" attri
}, 13);
});
+asyncTest('unchecked required checkbox should abort form submission', 1, function() {
+ var form = $('form[data-remote]')
+ .append($('<input type="checkbox" name="agree" required="required">'))
+ .removeAttr('data-remote')
+ .bind('ujs:everythingStopped', function() {
+ ok(true, 'ujs:everythingStopped should run');
+ })
+ .trigger('submit');
+
+ setTimeout(function() {
+ start();
+ }, 13);
+});
+
+asyncTest('unchecked required radio should abort form submission', 1, function() {
+ var form = $('form[data-remote]')
+ .append($('<input type="radio" name="yes_no" required="required" value=1>'))
+ .append($('<input type="radio" name="yes_no" required="required" value=2>'))
+ .removeAttr('data-remote')
+ .bind('ujs:everythingStopped', function() {
+ ok(true, 'ujs:everythingStopped should run');
+ })
+ .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 1e18432

Please sign in to comment.