Custom Validations

This section explores the concept of Custom Validations, offering a powerful way to extend the library's validation capabilities.

ReqEase's Form Validator offers robust validation capabilities, allowing you to define custom validation logic tailored to your specific needs.

Purpose

While ReqEase provides a rich set of built-in validation rules, there may be instances where you need to implement custom validation logic. Custom Validations empower you to do just that. These are JavaScript functions that you can define and attach to your form validation process.

How Custom Validations Work

A Custom Validation is a JavaScript function that adheres to a specific signature:

(callback: (validationSucceed: boolean) => void) => void
  • The function takes a single argument, callback, which is another function.

  • Inside your Custom Validation function, you implement your custom validation logic.

  • Once your logic is executed, you call the callback function, passing it a boolean value indicating whether the validation succeeded (true) or failed (false).

Here's a simplified example of a Custom Validation function:

function customValidationExample(callback) {
    // Custom validation logic
    const validationSucceed = /* Perform your custom validation checks here */;

    // Call the callback with the result
    callback(validationSucceed);
}

Adding Custom Validations

To utilize Custom Validations in ReqEase, you need to add them to your Form Validator configuration. This is done using the customValidations option, where you provide an array of your Custom Validation functions.

Here's how you add Custom Validations to your Form Validator:

const customValidationExample1 = function (callback) {
    // validation logic
    callback(true); //or false to indicate failure of validation.
};


// add Form Validator opions to reqEase Options:
const reqEase = new ReqEase({
        // Other options...
    validation: {
        // Other options...
        customValidations: [
            customValidationExample1,
            customValidationExample2,
            // Add more custom validations as needed
        ],
    }
});

Executing Custom Validations

During the validation process, ReqEase iterates through your list of Custom Validations. For each Custom Validation, it executes the validation logic. If a Custom Validation fails (returns false), the overall validation process stops immediately, preventing any subsequent validations or form submissions. If all Custom Validations succeed, the validation continues.

Custom Validations offer a powerful and flexible way to extend ReqEase's validation capabilities, allowing you to implement complex, domain-specific validation logic. Whether you need to check complex business rules or validate user input against unique requirements, Custom Validations give you full control over your validation process.

Last updated