Lotfiction
Lotfiction

Reputation: 359

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

Upvotes: 0

Views: 83

Answers (2)

frank
frank

Reputation: 3330

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

Upvotes: 1

GibboK
GibboK

Reputation: 73918

Yes, you can use use 'single tone object', or a property of the current object to add any information usefull for "validation" and re-access the value when you need it from NumberTextBox.

Upvotes: 0

Related Questions