Browse Source

#53: Fix notEmpty validator for radio and checkbox

nghuuphuoc 12 years ago
parent
commit
9f654d4d34
3 changed files with 18 additions and 6 deletions
  1. 11 0
      src/js/bootstrapValidator.js
  2. 1 0
      src/js/validator/callback.js
  3. 6 6
      src/js/validator/notEmpty.js

+ 11 - 0
src/js/bootstrapValidator.js

@@ -130,6 +130,17 @@
         // --- Public methods ---
 
         /**
+         * Retrieve the field elements by given name
+         *
+         * @param {String} field The field name
+         * @returns {null|jQuery[]}
+         */
+        getFieldElements: function(field) {
+            var fields = this.$form.find('[name="' + field + '"]');
+            return (fields.length == 0) ? null : fields;
+        },
+
+        /**
          * Validate the form
          */
         validate: function() {

+ 1 - 0
src/js/validator/callback.js

@@ -12,6 +12,7 @@
          *          // validator is instance of BootstrapValidator
          *      }
          * - message: The invalid message
+         * @returns {Boolean|Deferred}
          */
         validate: function(validator, $field, options) {
             var value = $field.val();

+ 6 - 6
src/js/validator/notEmpty.js

@@ -9,15 +9,15 @@
          * @returns {Boolean}
          */
         validate: function(validator, $field, options) {
-
-
             var type = $field.attr('type');
-            if('radio' == type) {
-                var radioSelector = "input[name=" + $field.attr('name') + "]:checked";
-                return ($(radioSelector).length > 0);
+            if ('radio' == type || 'checkbox' == type) {
+                return validator
+                            .getFieldElements($field.attr('name'))
+                            .filter(':checked')
+                            .length > 0;
             }
 
-            return ('checkbox' == type) ? $field.is(':checked') : ($.trim($field.val()) != '');
+            return $.trim($field.val()) != '';
         }
     };
 }(window.jQuery));