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
Parameter | Type | Description |
---|---|---|
controlId | String | The ID of the control for which to toggle the validator |
validatorName | String | The name of the validator to enable or disable |
enable | Boolean | true 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 emptyemail
- Validates that the field contains a valid email addressphone
- Validates that the field contains a valid phone numbernumber
- Validates that the field contains a numeric valuedate
- Validates that the field contains a valid datevat
- 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