Skip to content
Source
Sign inSign up
TanStack/form

Add an API to set errors #786

Send to my agent
Closedcrutchcorn(GitHub profile, opens in a new tab) opened this issue 5 comments
field.setErrorMap();

We cannot provide a setErrors API as it derives that state from the errorMap

And:

form.resetErrors();

I'm less certain about this API. Why do we need it? CC @samuelthng

  1. crutchcorn(GitHub profile, opens in a new tab) added labelstype: enhancement

  2. crutchcorn(GitHub profile, opens in a new tab) added labelshelp wanted

  3. crutchcorn(GitHub profile, opens in a new tab) added labelsgood first issue

  4. hello @crutchcorn

    I would like to work on this issue. Is there any advice or guide that you can give the correctly implement this feature ?

  5. I am trying this, but with recent TanStack From release, I am getting Uncaught TypeError: field.setErrorMap is not a function error.

     
    
    import { Textarea } from "@repo/ui/components/shadcn/textarea";
    import type React from "react";
    import { useEffect, useState } from "react";
    import { BaseField, type FieldControlProps } from "./base-field";
    import { useFieldContext } from "./context";
    
    type JSONFieldProps = React.ComponentProps<typeof Textarea> & FieldControlProps;
    
    export function JSONField({
      label,
      description,
      tooltip,
      tooltipSide,
      ...props
    }: JSONFieldProps) {
      const field = useFieldContext<Record<string, unknown> | undefined>();
      // const form = field.form; // Access the form instance from the field
      const isInvalid = field.state.meta.isTouched && !field.state.meta.isValid;
      const [text, setText] = useState(() =>
        field.state.value ? JSON.stringify(field.state.value, null, 2) : ""
      );
    
      useEffect(() => {
        const value = field.state.value;
        const next = value ? JSON.stringify(value, null, 2) : "";
        setText(next);
      }, [field.state.value]);
    
      const applyChange = () => {
        const trimmed = text.trim();
        if (!trimmed) {
          field.handleChange(undefined);
          field.setErrorMap({ onBlur: undefined });
          return;
        }
    
        try {
          const parsed = JSON.parse(trimmed);
          if (!parsed || typeof parsed !== "object") {
            throw new Error("Invalid JSON");
          }
          field.handleChange(parsed as Record<string, unknown>);
          field.setErrorMap({ onBlur: undefined });
        } catch (error) {
          const message = error instanceof Error ? error.message : "Invalid JSON";
          field.setErrorMap({ onBlur: message });
        }
      };
      return (
        <BaseField
          description={description}
          label={label}
          tooltip={tooltip}
          tooltipSide={tooltipSide}
        >
          <Textarea
            aria-invalid={isInvalid}
            className="field-sizing-fixed min-h-0"
            id={field.name}
            name={field.name}
            onBlur={() => {
              applyChange();
              field.handleBlur();
            }}
            onChange={(event) => setText(event.target.value)}
            value={text}
            {...props}
          />
        </BaseField>
      );
    }
    
  6. @xmlking already fixed in the latest release.

Sign in to join this conversation.