Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

The best way to create dynamic arrays with unform #409

Open
Michel7w opened this issue Jun 14, 2021 · 5 comments
Open

The best way to create dynamic arrays with unform #409

Michel7w opened this issue Jun 14, 2021 · 5 comments

Comments

@Michel7w
Copy link

Description of issue

Hello guys, i checked your documentation and saw that they mention how to work with complex structures and arrays in unform, but they don't talk about working with arrays dynamically, follow the documentation link.

https://unform.dev/guides/complex-structures

Usage example

What I want to do is allow the user to fill in a form that contains arrays, but I don't want to set the number of objects, I want it to be up to the user.

Example: having a form with an array and a plus button, and as the user clicks on the plus button, more inputs appear.

@rafaellupo
Copy link

@diego3g could you help us with this use case? How to deal with 1-n relationships where user can dynamically create input fields?
I was thinking in something like "Everything with same name inside a scope would return an array."

@LorhanSohaky
Copy link

any news?

@mementoyara
Copy link

This seems like a forgotten lib.
Sad because it is an amazing lib.

@FelipeArisi
Copy link

For create dinamyc items, we can use a function on registerField

I generate an array based on a text input

    useEffect(() => {
        registerField({
            name: fieldName,
            ref: inputRef.current,
            getValue: ref => {
                return convert_to_array(ref.value);
            },
            setValue(ref, value) {
                ref.value = value;
            }
        });
    }, [fieldName, registerField]);

What I couldn't do, was to correctly use the error validation of this array

@LorhanSohaky
Copy link

For create dinamyc items, we can use a function on registerField

I generate an array based on a text input

    useEffect(() => {
        registerField({
            name: fieldName,
            ref: inputRef.current,
            getValue: ref => {
                return convert_to_array(ref.value);
            },
            setValue(ref, value) {
                ref.value = value;
            }
        });
    }, [fieldName, registerField]);

What I couldn't do, was to correctly use the error validation of this array

could you create an example in codesandbox? I couldn't understand the idea very well.

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

No branches or pull requests

5 participants