Global Configuration

Configure error messages, date format, & validation strategy globally.

This page explores the configure method used to define global settings for form validation in your SPA application. Effectively configuring these options can streamline your development process.

Understanding the Configuration Options:

The configure method accepts a single argument of type FormConfig. This object allows you to set various properties that influence form validation behavior throughout your application. Let's break down the key configuration options:

  1. errorMessage (Optional):
    • This property allows you to define default error messages for specific validators used within your forms. It's an object where the key represents the validator name (e.g., "required") and the value is the corresponding error message displayed to the user when that validation rule fails (e.g., "This field is required").

    Benefits:
    Consistency: Establishes consistent error messages across your application, ensuring a clear and unified user experience.
    Reduced Code Duplication: Avoids the need to define the same error message for the same validator repeatedly within individual form components.
  2. dateConfig (Optional):
    This property is used to configure the date format for date fields within your forms.It's validate the date fields as per the configuration, It's an object with two optional properties:
    • separator (string): Defines the separator used between date components (e.g., "/" for "mm/dd/yyyy").
    • format (string): Specifies the format for displaying dates. Possible values include:
      "mdy": Month-Day-Year (e.g., "01/31/2024")
      "dmy": Day-Month-Year (e.g., "31/01/2024")
      "ymd": Year-Month-Day (e.g., "2024/01/31")

    Benefits:
    Standardized Date Validation: Ensures consistent date formatting across your application, improving user comprehension.
  3. runValidatorStrategy (Optional):
    This property defines the strategy for running form validation within your application. It's an enum with the following options:
    • RunValidatorStrategy.None (default): Runs on every value change.
    • RunValidatorStrategy.OnSubmit: Runs validation only when the form is submitted (e.g., upon clicking a submit button).

    Benefits:
    Flexibility: Allows you to tailor the validation behavior to your specific needs. For example, RunValidatorStrategy.None provides real-time feedback to users as they interact with the form, while RunValidatorStrategy.OnSubmit is suitable for scenarios where validation upon form submission is sufficient.

Syntax Example:

Here's a code snippet demonstrating how to use the configure method with some of the options explained earlier:

  import { configure } from "@nattyjs/tidy"; 
    
    configure({
      errorMessage: {
        validator:{
          required: 'This field is mandatory.'
        },
      },
      dateConfig: {
        separator: '-',
        format: 'ymd',
      },
      runValidatorStrategy: RunValidatorStrategy.OnSubmit,
    });