Knockout-Contrib / Knockout-Validation

A validation library for Knockout JS
1.02k stars 379 forks source link

Conditional Validation with Native Rules #627

Open coderbond opened 8 years ago

coderbond commented 8 years ago

Is it possible to use Conditional Validation along with Native Rules? Something like this for example.

            self.validationObject = ko.validatedObservable({
                Name: self.Name.extend({ required: true }),
                Type: self.Type.extend({ required: true }),
                Description: self.Description.extend({ required: true }),
                Price: self.Price.extend({
                    required:
                        {
                            number: true,
                            onlyIf:
                              function () {
                                  if (self.Classification() == 0){return false;} else{return true;}
                              }
                        }
                })
            });
andrew-locklair commented 8 years ago

Yes, although I'm not sure if the "required" would support the "number" property. I think it needs to be something like this:

number:
{
  params: true,
  onlyIf:
    function () {
         // etc