Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

knockout-validator

flut130MIT0.1.2TypeScript support: included

Painless form validation management using knockout.js

knockout, validator, forms, form validation, typescript, input

readme

Travis Code Climate Coveralls npm npm

knockout-validator

Add a description here...

Installation

npm

npm i -S knockout-validator

other

Browser, amd, commonjs, umd, systemjs and es6 versions of this module are available on the Github Releases.

Usage

TODO

Documentation

TODO

Building

In order to build knockout-validator, ensure that you have Git and Node.js installed.

Clone a copy of the repo:

git clone https://github.com/flut1/knockout-validator.git

Change to the knockout-validator directory:

cd knockout-validator

Install dev dependencies:

npm install

Use one of the following main scripts:

npm run build           # build this project
npm run generate           # generate all artifacts (compiles ts, webpack, docs and coverage)
npm run typings            # install .d.ts dependencies (done on install)
npm run test-unit        # run the unit tests
npm run validate        # runs validation scripts, including test, lint and coverage check
npm run lint            # run tslint on this project
npm run doc                # generate typedoc and yuidoc documentation
npm run typescript-npm    # just compile the typescript output used in the npm module

When installing this module, it adds a pre-commit hook, that runs the validate script before committing, so you can be sure that everything checks out.

Contribute

View CONTRIBUTING.md

Changelog

View CHANGELOG.md

Authors

View AUTHORS.md

LICENSE

MIT © Floris Bernard

changelog

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(); // returns true if both rules are valid
validator.fields('password').rule('minLength').isValid(); // returns true if validateMinLength() returned true
validator.fields('password').rule('containsNumbers').isValid(); // returns true if checkNumbers() returned true

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">
// in your ViewModel
import {InputGroupValidation} from 'knockout-validator';
public dateOfBirth = new InputGroupValidation(values =>
{
   // values are available as values.day, values.month and values.year
   ...
   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:

<!-- without shorthand !-->
<input type="text" name="day" data-bind="validationRule: validateDay, validationGroup: dateOfBirth, validateOn: 'value', validateWith: signupValidator">
<!-- with shorthand !-->
<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:

/* OLD EXAMPLE -- THIS CODE NO LONGER WORKS */
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