Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear error on Knockout-Validation

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.

like image 421
Clarence Klopfstein Avatar asked Dec 05 '12 14:12

Clarence Klopfstein


4 Answers

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

like image 90
Pure Function Avatar answered Sep 18 '22 17:09

Pure Function


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.

like image 34
Yooz Avatar answered Sep 21 '22 17:09

Yooz


Found the answer by implementing this Pull Request.

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

Gives me the feature I need.

like image 22
Clarence Klopfstein Avatar answered Sep 21 '22 17:09

Clarence Klopfstein


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.'
        }
    ]
});
like image 28
Yamo93 Avatar answered Sep 21 '22 17:09

Yamo93