2
votes

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?

2
You are at risk of being down-voted and closed - can you post any code of what you have tried and what is not working? This should be fairly simple if you have used Knockout validation before. - PW Kad
I am trying to figure out the helping part of your comment. Are you practically working with durandal, knockout and knockout validation within a SPA application? If not, thank you for reading the question but then you are not the desired adressee. Have a great day and thanks for contributing. - qhaut
Yep, sure am / do. This question does not follow the standards here for stack overflow - PW Kad
Well, then thank you in advance. Have a great day, Günther - qhaut
Check here github.com/Knockout-Contrib/Knockout-Validation/issues/259 - Basically Knockout validation doesn't play well with require.js, which Durandal uses during development and before you optimize your app. Not beautiful but should answer your question - PW Kad

2 Answers

2
votes

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

4
votes

Here is an working example of knockout validation and durandal vm;

define(['plugins/router', 'durandal/app', 'config'],function (router, app, config) {

    ko.validation.init({
        messagesOnModified: false
    });
    ko.validation.registerExtenders();
    var email = ko.observable().extend({
        required: { message: 'You must enter a valid email id' },
        email: { message: 'Email address you entered is not valid' }
    });
    var password = ko.observable().extend({
        required: { message: 'Enter password, minimum of 6 characters' },
        minLength: 6
    });

    var vm = {
        activate: activate,
        attached: attached,
        router: router,
        email: email,
        password: password,
        signIn: signIn,
        viewUrl : 'signin/index.html'
    };

    vm.errors = ko.validation.group(vm);
    return vm;

    function activate(id, querystring) {}

    function attached(view, parent) { }

    function signIn() {
        if (!vm.isValid()) {
            vm.errors.showAllMessages(true);
            return false;
        }
        //rest of logic         
    }
});