如何在UI中将ko.validation错误绑定在一起

时间:2013-11-22 10:15:41

标签: knockout.js requirejs durandal single-page-application knockout-validation

使用knockout绑定将错误绑定到UI页面时,我遇到了一些小问题。请帮帮我

 var searchObj = { model defination }

     if (!ko.validatedObservable(searchObj).isValid()) {
                        ko.validation.group(searchObj);
                        return false;
      }

    <ul data-bind='foreach: $root.errors' class="message">
         <li>
            <span data-bind='text:  $data'></span>
         </li>
    </ul>

     ko.validation.init({
            registerExtenders: true,
            decorateElement: true,
            errorElementClass: 'errorMessage',
            insertMessages: false,
            // messagesOnModified: false,
           // errorsAsTitle: true
        });

1 个答案:

答案 0 :(得分:0)

<div class="errorSummary" data-bind="if: searchCriteria.Errors().length">
      <ul data-bind='foreach: searchCriteria.Errors'>
            <li>
                <div data-bind='text: $data'></div>
            </li>
       </ul>
</div>

 ko.validation.init({
        registerExtenders: true,
        decorateElement: true,
        errorElementClass: 'errorMessage',
        insertMessages: false,
        messagesOnModified: false
        // errorsAsTitle: true
    });

return {
Errors: searchObj.errors;
}