Skip to main content

enableValidator

This function allows you to dynamically enable or disable specific validators for form controls. It's useful when you need conditional validation logic that changes based on user input or application state.

Syntax

app.enableValidator(controlId, validatorName, enable)

Parameters

ParameterTypeDescription
controlIdStringThe ID of the control for which to toggle the validator
validatorNameStringThe name of the validator to enable or disable
enableBooleantrue to enable the validator, false to disable it. Default is true if omitted

Return Value

This function doesn't return a value. It directly modifies the validation state of the specified control.

Common Validator Names

  • notEmpty - Validates that the field is not empty
  • email - Validates that the field contains a valid email address
  • phone - Validates that the field contains a valid phone number
  • number - Validates that the field contains a numeric value
  • date - Validates that the field contains a valid date
  • vat - Validates that the field contains a valid VAT number

Examples

Example 1: Disable the "notEmpty" validator for a field:

// Disable the required field validation for firstName
app.enableValidator("firstName", "notEmpty", false);

Example 2: Re-enable a previously disabled validator:

// Re-enable the email validation for emailAddress field
app.enableValidator("emailAddress", "email", true);

Example 3: Make a field required based on a condition:

// If company type is "corporation", make the companyNumber field required
if (app.getFieldValue("companyType") === "corporation") {
app.enableValidator("companyNumber", "notEmpty", true);
} else {
app.enableValidator("companyNumber", "notEmpty", false);
}

Notes

  • The validator states are stored in the app's validatorStates object
  • After changing validator states, the form's validation is reinitialized automatically
  • This function is typically used in event handlers like the change event
  • Validators are defined in the app's validator configuration during app initialization