0

I am using knockout validation and here is how my model looks like

function SignInViewModel() {

    var self = this;
    self.userName = ko.observable('').extend({
        required: true,
        pattern: {
            message: 'Username must be a valid email address',
            params: /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/
        }
    });
    self.password = ko.observable('').extend({
        required: true
    });

    self.errors = ko.validation.group(self);
    self.login = function (e) {

        if (self.errors().length == 0) {

            $.post("/account/jsonlogin", { userName: self.userName(), password: self.password(), returnUrl: "/" })
                .done(function (result) {
                    redirect(result.redirect);
                }).error(function () {

                });
        } else {

            self.errors.showAllMessages();
        }
    }

}
ko.validation.configure({
    insertMessages: true,
    decorateElement: true,
    errorElementClass: 'error'
});
$(function () {
    ko.applyBindings(new SignInViewModel());
});

When I put an invalid email address in userName and call login by clicking my submit button then knockout appends a error message span right next to element but it's set to display none. What is the problem.

Thanks

4

3 回答 3

0

你能用这个小提琴重现你的问题吗:

http://jsfiddle.net/jearles/hRa2h/

--

这对我有用。您会立即看到错误计数为 2,然后按“登录”将显示错误。键入无效的电子邮件会显示电子邮件。

====

HTML

<fieldset>
    <legend>User: <span data-bind='text: errors().length'></span> errors</legend>
    <label>User name: <input data-bind='value: userName'/></label><br/>
    <label>Password: <input data-bind='value: password' type="password"/></label>

</fieldset>
<button type="button" data-bind='click: login'>Login</button>

JS

ko.validation.configure({
    insertMessages: true,
    decorateElement: true,
    errorElementClass: 'error',
    messagesOnModified: false    
});

function SignInViewModel() {

    var self = this;
    self.userName = ko.observable().extend({
        required: true,
        pattern: {
            message: 'Username must be a valid email address',
            params: /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/
        }
    });
    self.password = ko.observable().extend({
        required: true
    });

    self.errors = ko.validation.group(self);
    self.login = function (e) {

        if (self.errors().length == 0) {
            alert('No errors');
        } else {

            self.errors.showAllMessages(true);
        }
    }

}
$(function () {
    ko.applyBindings(new SignInViewModel());
});
于 2013-06-26T10:39:08.367 回答
0

问题是由于 bootstrap ,因为 bootstrap 有一个类

.input-append, .input-prepend {
   margin-bottom: 5px;
   font-size: 0;
    white-space: nowrap;
 }

font-size:0造成了问题。淘汰赛按预期工作正常。

于 2013-06-27T05:25:39.503 回答
0

''从您的代码中删除

self.userName = ko.observable('').extend({

在你的代码中

上面的行应该更像这样:

self.userName = ko.observable().extend({

错误消息将显示在 span 标签中。

于 2015-02-19T17:28:34.007 回答