Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

custom validation rule is not triggered

I'm using the areSame rule from here:

ko.validation.rules['areSame'] = {
    getValue: function (o) {
        return (typeof o === 'function' ? o() : o);
    },
    validator: function (val, otherField) {
        return val === this.getValue(otherField);
    },
    message: 'The fields must have the same value'
};

And apply it like this:

this.confirm = ko.observable().extend({
    areSame: {
       params:this.password
    }
});

But it's never even triggered. I put debugger into validator function of the rule definition: validator: function (val, otherField) { debugger return val === this.getValue(otherField); }, however the flow never visits this point. What could be wrong?

EDIT:

The problem of not triggering validation is solved by calling ko.validation.registerExtenders();, however the rule doesn't work as expected. The problem is that the otherField variable, that is passed to validator, is the object {params:*observable here*}, where as the method getValue doesn't expect that as you can see from the source code. So either the source code is wrong or I defined the params for the rule in the wrong way. So which one?

like image 236
Max Koretskyi Avatar asked Mar 17 '14 13:03

Max Koretskyi


People also ask

How do I activate validation rules?

From the management settings for the relevant object, go to Validation Rules. Click Edit next to the rule you want to activate. To activate the rule, select Active, and save your changes. To deactivate the rule, deselect Active, and save your changes.

When validation rules are triggered in Salesforce?

Validation rules trigger every single time there's an attempt to save the record. The required conditions of the rule are indicated in formulas. The formulas contain one or more criteria that should be met in order to pass verification and save the record.

Does validation rule run before trigger?

As you said there is a custom validation rule by order of execution the custom validations run after running the before triggers and this is something that make sure the process is maintained to learn more you can try checking this link: developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…


1 Answers

Although it is not explicitly stated in the Wiki (it is in the sample code but not in the description) but

you need to call ko.validation.registerExtenders()

after you have defined your custom rules in order to register them:

ko.validation.rules['areSame'] = {
    getValue: function (o) {
        return (typeof o === 'function' ? o() : o);
    },
    validator: function (val, otherField) {
        return val === this.getValue(otherField);
    },
    message: 'The fields must have the same value'
};

ko.validation.registerExtenders();

In order to use your custom rule you don't need the "params syntax" so you can just write:

this.confirm = ko.observable().extend({
    areSame: this.password
});

If you want to use the "params syntax" you need to provide a custom error message property, otherwise the plugin does not correctly interprets the otherField argument :

this.confirm = ko.observable().extend({
    areSame: {
       params: this.password,
       message: 'a custom error message'
    }
});
like image 88
nemesv Avatar answered Sep 28 '22 03:09

nemesv