Reputation: 447
I am working on a project where I am trying to map constraints onto Dijit controls in an XSLT to HTML.
One such constraint is the maximum length of a text field. However, I cannot find such a constraint within the Dojo constraint language.
Does such a constraint (excluding a RegEx one) exist that I have missed in my example below?
<label for="shortField">shortField </label>
<input id="shortField" data-dojo-type="dijit/form/ValidationTextBox"
data-dojo-props = "name: 'shortField',
width:400,
height:150,
value: '1234567890',
required: true,
constraints: { max:10, maxLength:10, length:10, size:10 }
" />
For consistency, I would prefer to make use of a constraint rather than setting the maxLength property of the _TextBoxMixin.
(Unfortunately, the setting of a regex in the constraints is also undesirable, as that may be overridden later, outside of my control.)
Upvotes: 2
Views: 2287
Reputation: 447
It appears that no such constraint exists. furthermore, since I found out that I additionally need to validate the minimum length, I have chosen the solution of subclassing the Dijit.Form.ValidationTextBox.
This is the implementation I have chosen for the isValid method ,which reads from the constraints, so I can keep my generating XSL code consistent. )
isValid : function(){
var ancestorsValid = this.inherited(arguments);
if(ancestorsValid){
//Acquire only meaningful validation boundaries.
var minLength = this.constraints && this.constraints.minLength ? Number(this.constraints.minLength) : null;
var maxLength = this.constraints && this.constraints.maxLength ? Number(this.constraints.maxLength) : null;
//Validate min and max if present.
return ((minLength === null) || this.value.length >= minLength) &&
((maxLength === null) || this.value.length <= maxLength);
}
return false;
}
Upvotes: 2