#Get type of form data after validation.

9 messages · Page 1 of 1 (latest)

buoyant tiger
#

Is it possible to get the values of a validated form data?

For example:

const form = useForm({
  mode: 'uncontrolled',
  
  initialValues: {
    name: data.name ?? '',
    age: data.email ?? 0,
    countryId: data.countryId ?? null,
  },
  validate: {
      name: isNotEmpty('Name is required'),
      age: isInRange({ min: 18 }, 'You must be at least 18 to register'),
      countryId: isNotEmpty('Country is required'),
  }
});

In this case, type FormValues = typeof form.values; will give out

{
  name: string;
  age: number;
  countryId: string | null;
}

I need countryId to be a string only since validating it with isNotEmpty will prevent having an initial null.

buoyant tiger
#

I guess what I meant is, how do I transform that?

calm cairn
#

transformValues: (values) => ({ countryId: values.countryId! })

buoyant tiger
calm cairn
#

How is that different from transform values?

buoyant tiger
calm cairn