Search code examples
knockout.jsrequirejssingle-page-applicationdurandalknockout-validation

Configuring Knockout validation in Durandal / SPA application


I am not able to get knockout.validation up and running with durandal. knockout itself is working fine. If anyone got this straight I would be thankful for posting the configuration.

// main.js
require.config({
    paths: {
    "libs": "../scripts",
    "knockout": "../scripts/knockout-2.2.1",
    'knockout.validation': '../scripts/knockout.validation'
},
shim: {
    'knockout.validation': {
        deps: ["knockout"]
    }
}
});

define(function (require) {
validation = require('libs/knockout.validation')
// other dependencies are omitted
ko.validation = validation;
// ko works fine
// ko validation has been set
}

// my viewmodel
define(['services/logger',
'durandal/app',
'durandal/system',
'durandal/plugins/router',
'services/dataservice'],
function (logger, app, system, router, dataservice) {

var user_name = ko.observable().extend( {required: true } );
var user_password = ko.observable().extend( {required: true });

// Unable to get property 'extend' of undefined or null reference
// the same happens, if I define '/libs/knockout.validation locally

Does kockout.validation behaves different than other plugins?


Solution

  • As I mentioned in the comments, knockout validation doesn't play well yet with require.js at the moment. Durandal relies on require.js during development until you optimize your app.

    This link details a work around for now.

    http://github.com/Knockout-Contrib/Knockout-Validation/issues/259