ericmbarnard/Knockout-Validation — Issue #30: Should everything except Required rule pass with empty string or undefined?
Author: grofit
Created: 2012-02-29 14:24:39+00:00
State: open

I was just looking at a scenario I have, where I have a large document which is saved and viewed by users. Now I have quite a few fields which are not required to be filled in, however if they are I need them to adhere to a certain rules.

Take the below as an example:

```
function SomeCarModel() {
this.Name = ko.observable().extend({ required: true, minLength: 6, maxLength: 20 });
this.Year = ko.observable().extend({ required: true, min: 1950, max: 2050 });
this.MaxSpeed = ko.observable().extend({ min: 30, max: 300 });
}
```

Now in the above example the Name and Year are both required, and also need to be within the relevant constraints. However the MaxSpeed is an optional property which would not need to be entered BUT if it were entered then I would require it to be between 30 and 300.

Forgetting the imaginary example above, I have a lot of these optional fields, and people frequently type things in and save it, then may come back later and remove the value entered. So in this case it would flag a validation error even though I have not set the validation to be required.