Clear error on Knockout-Validation

14,425

Solution 1

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

Solution 2

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.

Solution 3

Found the answer by implementing this Pull Request.

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

Gives me the feature I need.

Solution 4

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.'
        }
    ]
});
Share:
14,425
Clarence Klopfstein
Author by

Clarence Klopfstein

I am a .NET programmer located in Cincinnati Ohio. I've been programming in .NET for four years. I ride in my car with my I AM A PC license plate with pride.

Updated on June 12, 2022

Comments

  • Clarence Klopfstein
    Clarence Klopfstein about 2 years

    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.

  • Pure Function
    Pure Function over 11 years
    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! :)
  • Brian Rosamilia
    Brian Rosamilia about 11 years
    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.
  • Ross
    Ross about 10 years
    This works great, thank you! This should be in the ko validation docs.
  • aruno
    aruno about 10 years
    this is exactly what showAllMessages(false) does internally, as answered by @yoann
  • aruno
    aruno about 10 years
    note: make sure you have messagesOnModified: true in your configuration. if it's false then the message will show always
  • Muhammad Raheel
    Muhammad Raheel almost 10 years
    i feel this is the best of three