Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested "required" fields pass validation when they are undefined and/or empty #637

Open
alexqfredrickson opened this issue Nov 28, 2016 · 0 comments

Comments

@alexqfredrickson
Copy link

alexqfredrickson commented Nov 28, 2016

Observables within observable arrays which are "required" are passing validation even if they are undefined or empty.

To reproduce the issue, please see this JSFiddle. I have a form which collects information about dogs' names and breeds. The "Dog Breed" field is required. Also note that I have followed the examples here on how to validate nested values.

  function KnockoutViewModel() {
    var self = this;
    self.dogs = ko.observableArray([new Dog()]);
    self.submitForm = function() {
      console.log(self.dogs()[0].Breed());
    };
    
    self.validationObject = ko.validatedObservable({
        dogs: self.dogs
    });
  }

  function Dog() {
    var self = this;
    self.Name = ko.observable();
    self.Breed = ko.observable().extend({
      required: {
        params: true,
        message: "The dog's breed field is required."
      }
    });
    
    self.validationObject = ko.validatedObservable({
        Breed: self.Breed.extend({
            required: true
        })
    });
  }

To reproduce the issue:

  1. Submit the form without a value in the "Dog Breed" field. validation successful
  2. Enter a value into the "Dog Breed" field, delete it, and re-submit the form. validation successful
  3. Enter a value into the "Dog Breed" field, submit the form, delete the value again, and resubmit the form. validation failed

I would expect (3) but not (1) and (2). Is this the intended behavior?

Note: I originally encountered this issue and described it in #636, but I'd initially missed the part of the documentation that states that special configuration is required to validate observables within observable arrays.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant