javascript - 可以将 dojo 验证器函数存储到变量中以便稍后调用它

标签 javascript validation dojo xpages

要自定义 dojo NumberTextBox、dojo ValidationTextBox 的验证器...我需要将默认验证器存储在 js 上下文中的某个位置,以便以后能够调用它;自定义验证取决于默认验证器的结果。

可以用这种方式做到这一点,你能帮我做到吗?

非常感谢

代码示例:

var djNumberTextBox1 = dijit.byId('#{id:djNumberTextBox1}');
djNumberTextBox1.validator = function() {
    var valide = true;

//here I'd like to invoke the default (old) validator (something like next line)
//var valide = djNumberTextBox1.validate();//but this causes a too much recursion because validate() references the current function

//customisation depending on the default (old) validator result
var djButton1 = dijit.byId('#{id:djButton1}');
if(!valide){
    djButton1.setDisabled(true);
}else{
    djButton1.setDisabled(false);
}

return valide;};

最佳答案

您可以尝试以下代码吗:

var djNumberTextBox1 = dijit.byId('#{id:djNumberTextBox1}');

// store the validator in _oldValidator
djNumberTextBox1._oldValidator = djNumberTextBox1.validator;

djNumberTextBox1.validator = function() {
    var valide = true;


    // Run the old validator
    valide = djNumberTextBox1._oldValidator();

//customisation depending on the default (old) validator result
var djButton1 = dijit.byId('#{id:djButton1}');
if(!valide){
    djButton1.setDisabled(true);
}else{
    djButton1.setDisabled(false);
}

return valide;};

编辑 1:
将参数传递给验证器函数。

var djNumberTextBox1 = dijit.byId('#{id:djNumberTextBox1}');

// store the validator in _oldValidator
djNumberTextBox1._oldValidator = djNumberTextBox1.validator;

djNumberTextBox1.validator = function(value, constraints) {
    var valide = true;


    // Run the old validator with arguments
    valide = djNumberTextBox1._oldValidator(value, constraints);

//customisation depending on the default (old) validator result
var djButton1 = dijit.byId('#{id:djButton1}');
if(!valide){
    djButton1.setDisabled(true);
}else{
    djButton1.setDisabled(false);
}

return valide;};

编辑 2:
我认为对于 NumberTextBox 会调用 validate()。

var djNumberTextBox1 = dijit.byId('#{id:djNumberTextBox1}');

// store the validate in _oldValidate
djNumberTextBox1._oldValidate = djNumberTextBox1.validate;

djNumberTextBox1.validate = function() {
    var valide = true;

    // Run the old validate
    valide = djNumberTextBox1._oldValidate();

//customisation depending on the default (old) validate result
var djButton1 = dijit.byId('#{id:djButton1}');
if(!valide){
    djButton1.setDisabled(true);
}else{
    djButton1.setDisabled(false);
}

return valide;};

关于javascript - 可以将 dojo 验证器函数存储到变量中以便稍后调用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28412729/

相关文章:

javascript - ChartJS - 点的大小不会改变

javascript - ....未捕获的类型错误 : Cannot read property '__e3_' of null

java - @Valid 不适用于嵌套对象(Java/Spring Boot)

jquery - jquery ui 对话框后面的 dijit datetextbox

javascript - 在 Dojo 根之外对 JavaScript 函数进行 DOH 命令行测试

javascript - Ajax 成功内的 Html Helper 中无法访问结果变量

javascript - 需要一个JSON对象,数组或literal.json

JavaScript 函数没有响应

Javax 验证不给出错误消息

javascript - CBC 模式下的 crypto-js DES(或三重 DES)加密