views:

83

answers:

2

I would like to disable client side validation on certain fields in my user form. Currently I have two sets of fields that are displayed depending on the value of a previous drop down list. i.e. if the drop down list is set to value "A" 1 new field appears in the form. If the drop down list is set to value "B" 3 new fields appear in the form (mutually exclusive from the new form field when "A" is selected). Currently my Dojo client side validation fails because the fields that are not shown to the user (and thus no data can be inserted into those fields) fails to validate.

Currently I determined that I can set the "validate" attribute to return true like so:

<input type="text" id="companycity" name="companycity" class="textinput" value="<?php echo set_value('companycity'); ?>" style="<?php if(isset($errorData['companycity'])){echo $errorData['companycity'];} ?>"
            dojotype="dijit.form.ValidationTextBox"
            required="true" trim="true" validate='return true'"
            regexp="([a-zA-Z]{1,25})"
            invalidMessage="Invalid value.  Must be between 1 and 25 alphabetic characters long.">

This fixes my issue for hidden fields. However this now means that no validation is performed when this field becomes visible to the user (i.e. the validate attribute is still set to return true).

I have tried removing the validate property when a field is displayed to the user like so:

dijit.byId('companycode').attr('validate','');

This just set the attribute to nothing. This however gives errors in firebug saying validate method not found, so I take that to mean I did not remove this attribute correctly or removing this attribute is not the appropriate way to do this.

I have also looked at overriding the validator method here but this doesnt seem like what I want either. I do not want to have to rewrite all the validation methods in place of dojo's.

I just want dojo not to validate if the field is not visible to the user. Thanks for any advice or help.

+1  A: 

I determined that I was doing this incorrectly. 'validate' and 'validator' are not appropriate attributes for the dojo ValidationTextBox. They both throw errors in Firebug of the form "this.validate is not a function". So at first it looked like this was working for me in the sense it was no longer trying to validate fields, but this is only because errors were being throw.

A better solution was to set the "required" attribute to false upon load.

<input type="text" id="companycode" name="companycode" class="textinput" value="<?php echo set_value('companycode'); ?>" style="<?php if(isset($errorData['companycode'])){echo $errorData['companycode'];} ?>"
            dojotype="dijit.form.ValidationTextBox"
            required="false" trim="true" 
            regexp="(^[A-Za-z]{2}-[0-9]{5}$)"
            invalidMessage="Invalid value.  Must be a valid company code of the form EX-00000">

Then when a field became visible, set the required attribute back to true.

dijit.byId('companycode').required = true
Eric LaForce
+2  A: 

ValidationTextBox expects validator to be a function, so while you can give it a string value in your markup, what's actually happening is that string is being converted to a function when the instance is created. Setting required is the right way to go, but to be safe I'd suggest using

dijit.byId("companycode").attr("required", true/false)

..to make sure state is correctly updated in the widget.

Sam Foster
I tried it that way initially, but it would no longer evaluate correctly. It was setting the required attribute to "true" (string) instead of true (bool)
Eric LaForce