remix-validated-form icon indicating copy to clipboard operation
remix-validated-form copied to clipboard

[Feature]: Single input with value as array

Open cjoecker opened this issue 1 year ago • 4 comments

What is the new or updated feature that you are suggesting?

For inputs like a multi-select, I would like to be able to be able to handle the value as a stringified array image

I have for example a single input called options. That input saves its value as 1,2,3 if I select the options 1,2,3 It would be nice if the library can understand that value as an array and not a string.

Why should this feature be included?

I couldn't find in the documentation a good way to handle array values from a single input. Creating hidden inputs for every single selected value is not that elegant in my opinion.

Here you have a code sandbox where you can reproduce what I mean: https://codesandbox.io/p/sandbox/gifted-mirzakhani-799ysl

cjoecker avatar Jun 09 '23 08:06 cjoecker

I could see support for this being included in the form of a helper for zod-form-data, but there isn't much we can do in remix-validated-form itself.

In general, the recommended approach for this type of thing is to transform the data in your validator. Since you're using zod, you can do it like this:

options: z.string().transform(val => val.split(','))

If you want to validate something like the number of options selected, you can also pipe it to another validator. You might also need to handle the empty string case (not familiar with MUI so not sure if it's necessary).

options: z.string()
  .transform(val => val === "" ? [] : val.split(','))
  .pipe(
    z.array(z.string()).min(1, "Must choose at least one")
  )

If we added a helper to zod-form-data, we could simplify the process a bit.

options: zfd.commaSeparated(
  z.array(z.string()).min(1, "Must choose at least one")
)

airjp73 avatar Jun 13 '23 15:06 airjp73

I think this issue is related to MUI how they handle it, because in react-select it works out of the box when used with remix-validated-form, correct me if im wrong.

jamalsoueidan avatar Jun 13 '23 20:06 jamalsoueidan

@airjp73 I like the idea of the helper. Having arrays as a string is something common. It can then be added to the arrays of documentation 👏

cjoecker avatar Jun 14 '23 13:06 cjoecker

If you want to validate something like the number of options selected, you can also pipe it to another validator. You might also need to handle the empty string case (not familiar with MUI so not sure if it's necessary).

options: z.string()
  .transform(val => val === "" ? [] : val.split(','))
  .pipe(
    z.array(z.string()).min(1, "Must choose at least one")
  )

@airjp73 Doesn't this generate a ZodIssue with a path that won't map back to the original field though (e.g. path: ["options", 0] vs original path: ["options"])?

silas avatar Jun 22 '23 16:06 silas

RVF v6 has been released 🎉

This use-case should be easily handled by state mode in v6. If it's still a problem in that version, please feel free to open a new issue.

You can find the documentation here and the migration guide here.

airjp73 avatar Aug 08 '24 20:08 airjp73