Changelog
v1.0.0
This version was based of the KnockoutValidator.ts class that has not been published to Github. As this is a complete rewrite, there are a lot of breaking changes. You can find differences between this class and knockout-validator v1.0.0 below.
Named validation rules
You can now apply multiple named validation rules to a single field. The example below validates with the functions validateMinLength() and checkNumbers() and the results named minLength and containsNumbers.
<input type="password" name="password" data-bind="validationRule: {minLength: validateMinLength, containsNumbers: checkNumbers}">
You can now access the validation state of the password input, but also of each field individually:
validator.fields('password').isValid();
validator.fields('password').rule('minLength').isValid();
validator.fields('password').rule('containsNumbers').isValid();
Bindings validateWith and validator merged
The validator binding that was used to indicate which KnockoutValidator instance to use for a single field, has been merged into the validateWith binding. The validateWith binding can now be applied both to a single field and a container element containing multiple fields.
Validation groups
You can now create InputGroupValidation instances to run validation based on the value of multiple fields. This replaces the global rule functionalities of the previous KnockoutValidation. One common example for group validation is validating a date of birth:
<input type="text" name="day" data-bind="validationRule: validateDay, validationGroup: dateOfBirth">
<input type="text" name="month" data-bind="validationRule: validateMonth, validationGroup: dateOfBirth">
<input type="text" name="year" data-bind="validationRule: validateYear, validationGroup: dateOfBirth">
import {InputGroupValidation} from 'knockout-validator';
public dateOfBirth = new InputGroupValidation(values =>
{
...
return isValid;
});
For a more detailed explanation please see the API documentation
Validate binding shorthand
Instead of using multiple bindings, you can now use the shortcut binding validate:
<input type="text" name="day" data-bind="validationRule: validateDay, validationGroup: dateOfBirth, validateOn: 'value', validateWith: signupValidator">
<input type="text" name="day" data-bind="validate: {rule : validateDay, group: dateOfBirth, on: value, with: signupValidator}">
Individual field validation
You can now manually trigger validation on a single field by calling validator.fields('fieldname').validate()
String return values of validation functions are no longer considered errors
In the previous KnockoutValidator, if a field needed to be validated for multiple rules you could return a string like so:
function validatePassword(password:string):string|boolean
{
if(password.length < 5)
{
return 'This password is too short';
}
if(password.match(/^[a-zA-Z]*$/))
{
return 'The password should contain at least one non-letter character';
}
return true;
}
You could then read the errors from the errors observable on a field. This approach was not very flexible as you could only return 1 error and it was difficult to apply localization on the error messages. In knockout-validator v1.0.0, a string return value is interpreted as true. This problem should now be solved with named rules (see above).
validate() always returns a Promise
The validate() method now always returns a Promise, even if there are no asynchronous rules attached to the validator's fields. Besides having a more consistent return value this allows the validator to make use of Knockout's deferred updates for better performance.
Validation state
- The isValid observable on the validator has been changed to a read-only computed value.
- The errors property of fields has been removed in favor of named validation rules
- The isValidated computed value of a field now also exists on the KnockoutValidator instance
KnockoutValidator options
- The validClassname, invalidClassname and validatingAsyncClassname options are now under the classnames property.
- The static ATTRIBUTENAMESPACE_ property has been removed
- The asyncTimeout is now called asyncValidationTimeout.
Requires Knockout 3.4.0 or up
Requires version 3.4.0 or newer because it uses Knockout's deferred updates and pure computed