Skip to content

Instantly share code, notes, and snippets.

@PEM-FR
Created March 1, 2011 16:03
Show Gist options
  • Save PEM-FR/849344 to your computer and use it in GitHub Desktop.
Save PEM-FR/849344 to your computer and use it in GitHub Desktop.
[enhancement] - dijit.form.ValidationTextBox : MorphTextBox with an example of textArea
dojo.provide("widget.custom.MorphBox");
dojo.require("dijit.form.ValidationTextBox");
dojo.require("dijit.form.Textarea");
dojo.declare(
"widget.custom.MorphBox",
dijit._Contained,
{
// morphValidationTest : function
// prends en paramètre une fonction utilisée pour effectuer un test
// particulier non géré, ou difficile à gérer en regExp.
// En général il faut lui préférer regExp ou regExpGen.
morphValidationTest : null,
validate: function(/*Boolean*/ isFocused){
// summary:
// Called by oninit, onblur, and onkeypress.
// description:
// Show missing or invalid messages if appropriate, and highlight textbox field.
// tags:
// protected
var message = "";
var isValid = this.disabled || this.isValid(isFocused);
// here we add the morphValidationTest
if(null != this.morphValidationTest){
// il faut tester si la propriété est une fonction ou pas
if(dojo.isFunction(this.morphValidationTest)){
isValid = (this.morphValidationTest(this.textbox.value) && isValid);
}
}
if(isValid){ this._maskValidSubsetError = true; }
var isEmpty = this._isEmpty(this.textbox.value);
var isValidSubset = !isValid && !isEmpty && isFocused && this._isValidSubset();
this.state = ((isValid || ((!this._hasBeenBlurred || isFocused) && isEmpty) || isValidSubset) && this._maskValidSubsetError) ? "" : "Error";
if(this.state == "Error"){ this._maskValidSubsetError = isFocused; } // we want the error to show up afer a blur and refocus
this._setStateClass();
dijit.setWaiState(this.focusNode, "invalid", isValid ? "false" : "true");
if(isFocused){
if(this.state == "Error"){
message = this.getErrorMessage(true);
}else{
message = this.getPromptMessage(true); // show the prompt whever there's no error
}
this._maskValidSubsetError = true; // since we're focused, always mask warnings
}
this.displayMessage(message);
return isValid;
},
setStateClass : function(/*string*/state){
this._state = state;
this._setStateClass();
}
}
);
dojo.declare(
"widget.custom.MorphTextBox",
[dijit.form.ValidationTextBox, widget.custom.MorphBox],
{}
);
dojo.declare(
"widget.custom.MorphTextareaBox",
[widget.custom.MorphTextBox, dijit.form.Textarea],
{
validator: function(/*anything*/value, /*dijit.form.ValidationTextBox.__Constraints*/constraints){
// summary:
// Overridable function used to validate the text input against the regular expression.
// Modified to accept \n and g line returns
// tags:
// protected
value = value.replace(new RegExp( "\\n", "g" ), " ");
return (new RegExp("^(?:" + this.regExpGen(constraints) + ")"+(this.required?"":"?")+"$")).test(value) &&
(!this.required || !this._isEmpty(value)) &&
(this._isEmpty(value) || this.parse(value, constraints) !== undefined); // Boolean
}
}
);
@PEM-FR
Copy link
Author

PEM-FR commented Mar 1, 2011

in order to use the morphValidationtest property, you could do something like that :
myTextareaDijit.set(
'morphValidationTest',
dojo.hitch(
this,
function(valeur){
return this._someCallbackFunction(valeur, anyArgsYouNeed);
}
)
);

@YaPeL
Copy link

YaPeL commented Mar 2, 2011

Well, I'm using your code, it works flawlessly, thanks :).

@PEM-FR
Copy link
Author

PEM-FR commented Mar 10, 2011

Also for dates, you could make checks like that :
dijitValeurDefaut.set(
'morphValidationTest',
dojo.hitch(
this,
function(valeur){
var newDate = new Date(valeur);
return ('' != valeur && !isNaN(newDate.getYear()));
}
)
);

@YaPeL
Copy link

YaPeL commented Nov 30, 2011

I'm having troubles under IE9 with your widget, and some custom accordions, it makes the explorer to close... :-/ any idea?

@PEM-FR
Copy link
Author

PEM-FR commented Dec 2, 2011

Hello YaPeL, what version of dojo are you using ? Because IE9 is not supported before 1.6.1 (which only "starts" supporting IE9, not fully).
Also I have not tested my code against dojo 1.7 yet, there might be some patches to do.
So :

  1. what version of dojo are you using ?
  2. what error / problem do you have ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment