views:

376

answers:

1

I'm new to dojo and could really use some help with the following 2 field validation examples.

In the following example of a dijit.form.ValidationTextBox field specifying the validator property seems to override the use of the regExp. (ie the field no longer adheres to the regExp rule). How to I make it do both?

<input dojoType="dijit.form.ValidationTextBox"
type="password"
name="password2"
id="password2"
maxLength="50"
trim="true"
regExp="[\w]+"
required="true"
validator="return this.value == dijit.byId('password').value"
invalidMessage="Confirmation password must match password" />

I have another similar example where one field depends on the value of another, but I don't have the syntax correct.

<input dojoType="dijit.form.ValidationTextBox" type="text"
name="homePhone"
id="homePhone"
style="width:20%"
maxLength="10"
trim="true"
required="false"
regExp="[\d]{10}"
validator="return (dijit.byId('preferredContactMethod').value == "home") && (this.value != null)"
invalidMessage="Home phone required (ie. 9198887777)"
/>

A: 

Correct; the default implementation of dijit.form.ValidationTextBox.prototype.validator() is to match this.value against this.regExp, as well as check against the various other constraints like this.required. Take a look at the source to see how it's done. If you override that, you're on your own to provide an implementation. Your implementation might choose to delegate to the prototype method and logically 'and' the results with your own tests. You could also override isValid, I suppose.

peller
So how are validation tests normally done when field1 depends on field2? Should I intercept the onSubmit event and provide the additional form validation tests there? Or is there a better way?
Adam
yes, it probably makes more sense to do the dependent validations on the form level rather than wiring together the individual widgets, if only because it would make a simpler user experience. Otherwise, you might end up with some circular logic that would make it harder to fill out the form. You can use dijit.form.Form which will let you set up a form-specific validation rule in addition to checking the valid states of the individual widgets.
peller