Created
March 1, 2011 16:03
-
-
Save PEM-FR/849344 to your computer and use it in GitHub Desktop.
[enhancement] - dijit.form.ValidationTextBox : MorphTextBox with an example of textArea
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
); |
Well, I'm using your code, it works flawlessly, thanks :).
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()));
}
)
);
I'm having troubles under IE9 with your widget, and some custom accordions, it makes the explorer to close... :-/ any idea?
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 :
- what version of dojo are you using ?
- what error / problem do you have ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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);
}
)
);