利用服务器端数据轻松进行客户端自定义验证 [英] Breeze client-side custom validation with server-side data

查看:101
本文介绍了利用服务器端数据轻松进行客户端自定义验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我创建了一个自定义验证器,用于检查数据库中是否使用了用户名。
验证的整个过程。

  function createExistingUsernameValidator(){
var name ='existingUsernameValidator';
var ctx = {messageTemplate:‘Questa partita I.V.A. o,displayName: Partita IVA o Codice Fiscale}; o codice taxe sonogiàstati inseriti。
var val = new Validator(name,valFunction,ctx);

返回值;

函数valFunction(value,context){
var result = ko.observable(true);
require('services / datacontext')。getIsUserByUsername(value,result)
.then(function(){
调试器;
return!result();
});
}
}

承诺有效:我知道,因为它触及了 debbugger 行,并且retunrnig值正确。

但是验证器的求值始终为 false ,因为调用验证器时我没有返回任何信息。换句话说:它不会等待诺言。

是我的JavaScript不好还是其他?

欢迎任何帮助。

谢谢!



答案后编辑

我来到了一个涉及敲除验证(非常有用的脚本)的解决方案。

 函数createIsExistingUserKoValidation(){
ko.validation.rules ['existingUsername'] = {
异步:true,
验证程序:函数(val,参数,回调){
if(val){
var result = ko.observable();
require('services / datacontext')。getIsUserByUsername(val,result)
.then(function(){
callback(!result());
});
}
},
消息:现有用户名。
};
ko.validation.registerExtenders();
}

在实体创建中:

  var createDitta = function(){
var ditta = manager.createEntity(entityNames.ditta,
{
id:newGuid(),
legaleRappresentante:createPersona(),
isAttiva:true
});
ditta.pivaCodFiscale.extend({existingUsername:{消息:‘现有用户名。’,params:true}});
ditta.pivaCodFiscale.isValidating(false);

返回ditta;
};

ditta.pivaCodFiscale.isValidating(false); 这是必需的,因为isValidating

解决方案

问题是写的 valFunction 总是返回'undefined '。 (这是'falsy'。



'return!result()'表达式不是'valFunction'的返回值,它只是匿名函数的结果

您要尝试的是编写一个异步验证这不受Breeze的支持,但是这个主意是个好主意。



我认为您可以通过使异步来完成所需的操作回调实际上是在实体上设置值,并让设置操作本身触发单独的同步验证。



这对于Breeze更自然地支持是个好主意因此请随时向 Breeze用户语音之类的异步验证之类的东西,我们以此来衡量社区对提议的各种功能/扩展功能。



I created a custom validator that check if a username is used on a DB. The whole process of validation works. What is not working is result.

function createExistingUsernameValidator() {
        var name = 'existingUsernameValidator';
        var ctx = { messageTemplate: 'Questa partita I.V.A. o codice fiscale sono già stati inseriti.', displayName: "Partita IVA o Codice Fiscale" };
        var val = new Validator(name, valFunction, ctx);

        return val;

        function valFunction(value, context) {
            var result = ko.observable(true);
            require('services/datacontext').getIsUserByUsername(value, result)
                .then(function () {
                    debugger;
                    return !result();
            });
        }
    }

The promise works: I know because it hits the debbugger line and the retunrnig value is correct.
But the validator always evaluate as false because I'm not returning anything when the validator is called. In other words: it won't wait for the promise.
Is it my bad javascript or something else?
Any help is welcome.
Thank you!

Edited after answer
I've come to a solution that involves Knockout Validation (very useful script).

    function createIsExistingUserKoValidation() {
        ko.validation.rules['existingUsername'] = {
            async: true,
            validator: function (val, params, callback) {
                if (val) {
                    var result = ko.observable();
                    require('services/datacontext').getIsUserByUsername(val, result)
                        .then(function () {
                            callback(!result());
                        });
                }
            },
            message: ' Existing username.'
        };
        ko.validation.registerExtenders();
    }

In the entity creation:

var createDitta = function () {
        var ditta = manager.createEntity(entityNames.ditta,
            {
                id: newGuid(),
                legaleRappresentante: createPersona(),
                isAttiva: true
            });
        ditta.pivaCodFiscale.extend({ existingUsername: { message: ' Existing username.', params: true } });
        ditta.pivaCodFiscale.isValidating(false);

        return ditta;
    };

ditta.pivaCodFiscale.isValidating(false); this is needed because isValidating is initialized with true.

解决方案

The problem is that your valFunction as written will ALWAYS return 'undefined'. ( which is 'falsy'.

The 'return !result()' expression is NOT the return value of 'valFunction', it is simply the result of an anonymous function that executes AFTER valFunction has already returned. This is the async nature of promises.

What you are trying is to write an 'asynchronous' validation which is NOT supported out of the box with Breeze, but the idea IS a good one.

I think that you might be able to accomplish what you want by having your async callback actually 'set' a value on the entity and have that set operation itself trigger a seperate 'synchronous' validation.

This IS a good idea for Breeze to support more naturally so please feel free to add a feature request to the Breeze User Voice for something like "asynchonous validation". We use this to gauge the communities interest in the various proposed features/extensions to Breeze.

这篇关于利用服务器端数据轻松进行客户端自定义验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆