简体   繁体   中英

It is possible to store a dojo validator function into a variable to invoke it later

To customize a validator of dojo NumberTextBox, dojo ValidationTextBox... I need to store the default validator somewhere in the js context and to be able to invoke it later ; the custom validation depends on the result of the default validator.

It is possible to do it in this way and can you help me doing it ?

Thanks a lot

A sample of code :

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

can you try the following code:

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

Edit 1 :
Passed arguments to the validator function.

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

Edit 2 :
I think for NumberTextBox the validate() is called.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM