Skip to content
Source
Sign inSign up
TanStack/form

Custom errors docs #1185

Send to my agent
Closedcrutchcorn(GitHub profile, opens in a new tab) opened this issue 3 comments
  • Return string values from the form
  • Return non-string values from the form
  • disableErrorFlat prop on fields
  • How type-safe TSF's errors and errorMap is
  1. crutchcorn(GitHub profile, opens in a new tab) added labelstype: docs

  2. Hey @crutchcorn wondering about this, when you mean return errors you mean we get something like this right for example by doing this

    Image

    in my validators of the form i would have this

       validators: {
          // Example 1: String error values
          onChange: ({ value }) => {
            return {
              fields: {
                username:
                  value.username && value.username.length < 3
                    ? "Username must be at least 3 characters"
                    : undefined,
                email:
                  value.email && !value.email.includes("@")
                    ? "Email must contain @"
                    : undefined,
              },
            };
          },
    
          // Example 2: Non-string error values
          onBlur: ({ value }) => {
            return {
              fields: {
                // Number error value
                age:
                  value.age < 18
                    ? 18 - value.age // Numeric value representing years below 18
                    : undefined,
    
                // Object error value with additional context
                email:
                  value.email && !value.email.endsWith(".com")
                    ? {
                        message: "Only .com emails are allowed",
                        severity: "warning",
                        code: 1001,
                      }
                    : undefined,
              },
            };
          },
    
          // Example 3: Mixed error value types
          onSubmit: ({ value }) => {
            return {
              fields: {
                // Array of errors
                username:
                  !value.username || value.username.length < 5
                    ? [
                        "Username is required",
                        "Username must be at least 5 characters",
                      ]
                    : undefined,
    
                // Boolean error value
                age:
                  value.age <= 0
                    ? true // Just a flag indicating an error
                    : undefined,
              },
            };
          },
        },

    if so i can write the docs for it

  3. crutchcorn(GitHub profile, opens in a new tab) assignedenyelsequeira

  4. The severity example kind of lead me to believe I could store warning/info messages in there and they should not block form submission or mark the field as invalid, but if I understand correctly this is not the case right (there's no way to specify which errors are "blocking") ?

Sign in to join this conversation.