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

时间:2015-02-09 15:01:31

标签: 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;};

2 个答案:

答案 0 :(得分:1)

您可以尝试以下代码:

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;};

答案 1 :(得分:0)

是的,您可以使用“单音对象”或当前对象的属性来添加任何有用的信息,用于"验证"并在需要时从NumberTextBox重新访问该值。