Knockout-Contrib / Knockout-Contrib/Knockout-Validation

Why does clearError() mark model as valid?

Open
#565 4 comments 0 reactions 0 assignees View on GitHub
question
Dominant language
JavaScript
Stars
1k
Forks
366
PR merge metrics
No merged PRs in 30d

Description

``` javascript
ko.validation.init();
var myValue = ko.observable().extend({ required: true, minLength: 3 });

console.log(myValue.isValid()); // false
myValue.clearError();
console.log(myValue.isValid()); // true - I believe this is wrong
```

Use case:
I have a form in a dialog, with a required field and some other rule. messagesOnModified = true
On fresh start, when submitted empty, KO Validation treats the model as invalid, all is good.

User types in an invalid value, error shows up. He clicks cancel and the form is hidden.
I clear the value and errors with myVal(null) and myVal.isModified(false).clearError().

He opens the form again and submits it right away empty. KO Validation will wrongly report the model as valid. The reason behind this wrong behaviour is that the previous call to clearError() not only removed the error message, but also marked the field as valid which wasn't true.

I know I can work around this by using myVal.error(null) instead of clearError().

Contributor guide

Open the contributing guide

Research direction

Start at the clearError() entry point and trace how it changes the observable's error and validity state. Reproduce the required and minLength example, then add a regression test covering clearing the value, resetting isModified, reopening the form, and submitting it empty; done means the field remains invalid.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend, testing-qa
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.