Skip to content

Using useValidation for Dynamically Added Forms #157

@trobic1

Description

@trobic1

Hi Enterwell team,

We’re using @enterwell/react-form-validation in our React project and love how useValidation works for static forms. However, we’re struggling with applying it to a scenario where we dynamically add form instances.

Problem

We have a feature where users can click a button to add new form instances to a list (e.g., a list of forms stored in a state array). Each form has fields like name (validated with isNonEmptyString) and type (validated with isNonEmptyArray). We want to validate all fields across all forms when the user submits, but we can’t call useValidation dynamically (e.g., inside a button click handler) due to React’s hook rules.

Currently, we’ve implemented a custom validation solution, but we’d prefer to use @enterwell/react-form-validation for consistency with other parts of our app.

Desired Outcome

We’d like to:
Dynamically add form instances on button click, each with fields validated using useValidation.
Validate all fields in all form instances at once (e.g., with a validateAll function) to check for errors and display them in the UI.

Questions
Is it possible to use useValidation for dynamically added forms while respecting React’s hook rules?
What’s the recommended approach for managing a list of forms and validating all their fields together?
Are there any examples or patterns for this use case with your package?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions