Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Single input with value as array #295

Open
cjoecker opened this issue Jun 9, 2023 · 4 comments
Open

[Feature]: Single input with value as array #295

cjoecker opened this issue Jun 9, 2023 · 4 comments

Comments

@cjoecker
Copy link

cjoecker commented Jun 9, 2023

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 cjoecker changed the title [Feature]: Array input values as strings [Feature]: Single input with value as array Jun 9, 2023
@airjp73
Copy link
Owner

airjp73 commented Jun 13, 2023

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")
)

@jamalsoueidan
Copy link

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.

@cjoecker
Copy link
Author

@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 👏

@silas
Copy link

silas commented Jun 22, 2023

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"])?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants