Why doesn't checkbox validation work in Parsley.js?

Hubro picture Hubro · Jun 29, 2015 · Viewed 8.9k times · Source

I can't for the life of me get Parsley.js to validate my checkboxes, even though my code looks just like their example. My HTML looks like this:

<div class='button-row'>
  <input data-parsley-mincheck='1' id='add-package-a-pre' name='add-package-alerts' type='checkbox' value='pre'>
  <label for='add-package-a-pre'>
    Pakke registrert
  </label>
  <input id='add-package-a-sent' name='add-package-alerts' type='checkbox' value='sent'>
  <label for='add-package-a-sent'>
    Pakke sendt
  </label>
  <input id='add-package-a-ready' name='add-package-alerts' type='checkbox' value='ready'>
  <label for='add-package-a-ready'>
    Pakke klar for henting
  </label>
  <input id='add-package-a-loaded' name='add-package-alerts' type='checkbox' value='loaded'>
  <label for='add-package-a-loaded'>
    Pakke lastet opp for utkjøring
  </label>
  <input id='add-package-a-delivered' name='add-package-alerts' type='checkbox' value='delivered'>
  <label for='add-package-a-delivered'>
    Pakke leveret
  </label>
</div>

Parsley works out of the box and prevents my form from submitting when I have an error in one of my text inputs, but it completely ignores the fact that none of my checkboxes are checked. As far as I can see, my code looks just like the example they provide: http://parsleyjs.org/doc/examples/simple.html

I have also made a JSFiddle that demonstrates the problem.

Can anybody see why it's not working? I've been stuck on this for hours now, and I can't find anybody else who's experiencing the same problem.

Answer

Hubro picture Hubro · Jun 29, 2015

It turns out that Parsley will always accept that no checkboxes are checked as long as the required attribute is not set, even if data-parsley-checkmin="1" is.

To elaborate:

  • <input type="checkbox" data-parsley-checkmin="1" />
    Will always pass validation, no matter what
  • <input type="checkbox" data-parsley-checkmin="2" />
    Will only fail validation if 1 checkbox is checked
  • <input type="checkbox" data-parsley-checkmin="1" required />
    Will fail validation if no checkboxes are checked

In other words, data-parsley-checkmin="1" is never useful. Just replace it with required.