TanStackT
TanStack5mo ago
24 replies
uncertain-scarlet

linked fields, source field validation uses stale linked values

i have this linked field logic, and for some reason i can't figure out how to get the validation trigger to use the newly set values.

i'm able to get around this by, instead of using linked fields, in the source field i just manually set the values there. everything works great then, but it's a bit unergonomic because this is an array field, so i have to manually iterate over each array entry and setFieldValue

<form.api.AppField
  key={split.memberUserId + view.value}
  name={`splits[${index}].${view.value}`}
  validators={{
    onChangeListenTo: ["amount"],
    onChange: (context) => {
      if (view.value === "percent") {
        const percent = 
          splitFieldContext.state.value.percent;
        const total = 
          splitFieldContext.form.state.values.amount;

        const updateFn = updateSplit({ 
          key: "percent", 
          value: percent,
          total: total 
        });

        splitFieldContext.setValue(updateFn);
      }
      // seemingly no matter how i trigger validation here,
      // the values set above ^ are not used during validation
      // although i see updated values in form state and ui 
      form.validateSomehowOrAnother()
    },
  }}
  children={(fieldContext) => (
    <div key={view.value} className="space-y-2"><fieldContext.SheetSplitField ... /></div>
  )}
/>
Was this page helpful?