Enabling and disabling validation in cfform

Built-in validation in cfform is great, but sometimes, it is a little inflexible. What happens if we want to require a State field only if the Country field is US? There is no "built-in" way to specify such a thing. But there is a simple technique we can use. As always, use these tips only if you know what you are doing ;), as they may have side effects.


Built-in validation in cfform is great, but sometimes, it is a little inflexible. What happens if we want to require a State field only if the Country field is US? There is no "built-in" way to specify such a thing. But there is a simple technique we can use. As always, use these tips only if you know what you are doing ;), as they may have side effects.

The key is to call the following when we want to disable the validation on a given field. We may want to call it when other field changes its value for example.

mx.validators.Validator.disable( this, "myFormName.myFieldName");

In order to enable it back, just call

mx.validators.Validator.enable( this, "myFormName.myFieldName");

Note that if the validation that you want to disable is “required”, you will not be able to get rid of the red star next to field.

The complete code:

mx.validators.Validator.disable( this, "myform.phone"); mx.validators.Validator.enable( this, "myform.phone");

View live example
Download the source