17

I have a page setup with Knockout.js and using Knockout-Validation.

During the page load I put another plugin on a select box which fires a change, which fires the validation. I need to be able to clear that error using JS so I can start with a fresh looking UI and give feedback on the form post or select box change.

I can't find anything that allows me to clear an error in Knockout-Validation.

Clarence Klopfstein
  • 4,682
  • 10
  • 33
  • 47

5 Answers5

31

Probably a better way that follows what is already implemented in knockout validation is to say property.isModified(false);

if you have a whole view model to reset simply loop through all the validated properties and call that isModified(false)

See the comment from Eric Barnard here

Hope that helps

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
Pure Function
  • 2,129
  • 1
  • 22
  • 31
  • Thanks. Also, this may be obvious to others but you have to do it after you clear the field. Example : _thisComment(''); _this.Comment.isModified(false). Easy validation reset. – Brian Rosamilia May 19 '13 at 05:05
  • 1
    this is exactly what `showAllMessages(false)` does internally, as answered by @yoann – Simon_Weaver May 07 '14 at 02:09
25

Late answer but if anyone needs it :

// assuming the ko.observable on the checkbox is called propBoolean
var propBooleanlValid = ko.validation.group(self.propBoolean, { deep: false });
propBooleanlValid .showAllMessages(false);

It will hide the message till the next validation.

Yooz
  • 2,506
  • 21
  • 31
4

Found the answer by implementing this Pull Request.

https://github.com/Knockout-Contrib/Knockout-Validation/pull/184

Gives me the feature I need.

Jake
  • 57
  • 7
Clarence Klopfstein
  • 4,682
  • 10
  • 33
  • 47
  • Not to offend but I feel like this answer is not the one people should be taking as action when they want this functionality. whence my downvote. I upvoted your question though, its a good one! :) – Pure Function Feb 07 '13 at 04:21
1

One way to solve it is by using a custom validator where you check for a flag in your viewmodel like "ignoreValidation". If that flag is true, then you let the validator pass.

Example of how that validator would look like:

viewmodel.userHasPremiumMembership.extend({
    validation: [
        {
            validator: function () {
                if (viewmodel.ignoreValidation) {
                    return true;
                }

                return viewmodel.userHasPremiumMembership();
            },
            message: 'User needs to have premium membership.'
        }
    ]
});
Yamo93
  • 514
  • 3
  • 12
0

If you use an entity manager make sure to not include the entityAspect in the validation since it is connected to all other entities. Also see How to rollback knockout validation errors?

Community
  • 1
  • 1
Stefan
  • 10,010
  • 7
  • 61
  • 117