bootstrap-validator icon indicating copy to clipboard operation
bootstrap-validator copied to clipboard

Custom validator with multiple checkboxes has erratic behavior

Open MatiasSM opened this issue 9 years ago • 19 comments

I've implemented a custom validator to be able to verify that at least one of many checkboxes is selected. However, sometimes when checking and un-checking the checkboxes, the submit button still appears as disabled and some other times, the checkboxes are still marked as with error (in red).

I was able to reproduce the issue in the following JS Bin: http://jsbin.com/litalocovi/1/edit?html,js,output

A sequence that seems to fail is:

  1. Check F1
  2. Un-check F1
  3. Check F2

We would expect that, in this case, the validation would be correct and the submit button is enabled, but it seems to fail (neither of those happen). The problem may be in the custom validator but I can not spot any error and it is a really simple piece of code.

Any clue?

MatiasSM avatar Mar 23 '15 23:03 MatiasSM

.form-group elements can only contain a single input element. Move each checkbox to its own .form-group

1000hz avatar Mar 23 '15 23:03 1000hz

According to Bootstrap, it seems as though checkboxes don't belong in a .form-group at all, but rather a .checkbox container. I'll extend the plugin to check for that as well as .form-group.

1000hz avatar Mar 23 '15 23:03 1000hz

Ok, thank you for your feedback. I will investigate further about the correct way of grouping checkboxes. Until now I treated this case (many checkboxes representing a unique group) as if they were radiobuttons, that's why I implemented it that way.

I stay tuned for the new .checkbox container support. Thank you for your prompt response!

MatiasSM avatar Mar 24 '15 01:03 MatiasSM

Do you know any workaround I can apply for a use case like mine? (that is, validate at least one checkbox in a group is selected)

MatiasSM avatar Mar 29 '15 22:03 MatiasSM

You'll need to get rid of the parent .form-group div and add a form-group class to each individual .checkbox div. This will screw up the styles a little, so you'll need to apply a margin-bottom to .form-group.checkbox:last-child.

Then to add the validator:

$('[data-toggle="validator"]').validator({
  custom: {
    chkgrp: function ($el) {
      var name = $el.data('chkgrp')
      var $checkboxes = $el.closest('form').find('input[name="' + name + '"]')

      return $checkboxes.is(':checked')
    }
  },
  errors: {
    chkgrp: 'Need at least one checked'
  }
}).on('change.bs.validator', '[data-chkgrp]', function (e) {
  var $el  = $(e.target)
  var name = $el.data('chkgrp')
  var $checkboxes = $el.closest('form').find('input[name="' + name + '"]')

  $checkboxes.not(':checked').trigger('input')
})

1000hz avatar Mar 30 '15 02:03 1000hz

Thank you very much for your feedback!

MatiasSM avatar Mar 30 '15 23:03 MatiasSM

So investigating this further, it seems as though no change to the plugin is needed.

Ignore what I said about adding the form-group class to each .checkbox. You should put all the checkboxes in a single .form-group container. Using the custom validator code I gave you above should do the trick.

1000hz avatar Apr 02 '15 05:04 1000hz

Thank you for the help, I finally got my code working.

MatiasSM avatar Apr 02 '15 18:04 MatiasSM

I have the same problem and tried the solution which you have provided, but it doesn't work for me: http://jsbin.com/tefipazace/2/edit?html,js,output

mwitzmann avatar May 28 '15 12:05 mwitzmann

you have marked all your checkboxes as required. Also you need a form-group for each checkbox. Here is a working example: http://jsbin.com/xosabirowu/1/edit?html,js,output

MatiasSM avatar May 28 '15 18:05 MatiasSM

Oh sorry, you are right. "required" was the problem. Now it works. Thank you.

mwitzmann avatar May 29 '15 10:05 mwitzmann

@1000hz, @MatiasSM any tips on how to get the submit button to show disabled on load (prior to any checkbox selection)?

brandomr avatar Aug 05 '15 20:08 brandomr

@1000hz your solution works in 0.9.0 but doesn't in 0.10.2: https://github.com/1000hz/bootstrap-validator/issues/96#issuecomment-87521676 any ideas?

maximelebreton avatar Jul 12 '16 14:07 maximelebreton

@1000hz here is a jsbin.com/resivujore with your https://github.com/1000hz/bootstrap-validator/issues/96#issuecomment-87521676 code on 0.10.2 version if you have time to look. thanks!

maximelebreton avatar Jul 15 '16 09:07 maximelebreton

@1000hz updated example with 0.11.5 jsbin.com/jinagorole

maximelebreton avatar Jul 18 '16 16:07 maximelebreton

Some changes to the validator plugin are preventing this from working. I suggest you stick with v0.9.0 until I can push out some changes to support this use case.

1000hz avatar Jul 18 '16 17:07 1000hz

Any updates regarding this coming up? I would love to have the newest version of the validator but this bug is holding me back at 0.9.0 :/

VirtualBoost avatar Dec 13 '16 07:12 VirtualBoost

+1

maximelebreton avatar May 29 '17 14:05 maximelebreton

+1

mordekasg avatar Dec 05 '18 23:12 mordekasg