Knockout-Contrib / Knockout-Contrib/Knockout-Validation

Problems with .isValid() on required checkboxes

Open
#598 13 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
JavaScript
Stars
1k
Forks
366
PR merge metrics
No merged PRs in 30d

Description

Hey guys! I experienced some problems when making checkboxes required (for example terms and conditions).

See here for reference: http://jsfiddle.net/v6d5y3or/

Expected behaviour: Form .isValid() should reflect the checkbox value: checkbox.true = form.true and checkbox.false = form.false

Actual behaviour: Form .isValid() is always true: checkbox.true = form.true and checkbox.false = form.true

I'm not really sure why this happens and one would think that required: true works together with the checked binding.

A temporary solution to me is to implement a custom validator to make it work as expected:

ko.observable(false).extend({
  validation: [{
    validator: (value, params) => Boolean(value)
  }]
})

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked JSFiddle reproduction and trace how required validation handles Knockout checkbox checked bindings. Compare the unchecked and checked cases against the reported expected and actual isValid() results; done means an unchecked required checkbox makes the form invalid while a checked one makes it valid.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.