Validator Callbacks

This section delves into Validator Callbacks, explaining how they work and how you can leverage them to enhance your validation experience.

Understanding Validator Callbacks

Validator Callbacks are functions in ReqEase that allow you to define custom behavior when the validation process encounters either a successful or failed outcome. These callbacks offer you the flexibility to respond to validation events according to your application's requirements.

Available Callbacks

ReqEase's Form Validator supports two main methods in callbacks:

  1. onSuccess: This callback is executed when the validation process succeeds, indicating that all validation rules and conditions have been met.

  2. onFailure: Conversely, this callback is invoked when the validation process fails, indicating that at least one validation rule or condition was not met.

Configuration Options

You can define your own custom callbacks by providing functions that take the Form Validator instance as a parameter. This allows you to perform specific actions when the validation process reaches either a successful or failed state.

Here's an example of defining custom callbacks:

const customCallbacks = {
    onSuccess: (formValidator) => {
        // Custom logic to execute on validation success
        // Access the Form Validator instance if needed
    },
    onFailure: (formValidator) => {
        // Custom logic to execute on validation failure
        // Access the Form Validator instance if needed
    }
};


// add Form Validator opions to reqEase Options:
const reqEase = new ReqEase({
        // Other options...
    validation: {
        // Other options...
        callbacks: customCallbacks,
    }
});

When to Use Validator Callbacks

Validator Callbacks are valuable when you need to react to the outcome of a validation process. Use them to trigger actions or display messages based on whether validation succeeded or failed. Typical use cases include showing error messages, disabling submission buttons, or triggering specific workflows depending on validation results.

Last updated