final-form / final-form/react-final-form

Is there a way to mutate field value inside the validator?

Open
#459 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
7.4k
Forks
497
PR merge metrics
No merged PRs in 30d

Description

Hi guys!

What I'm trying to achieve is basically async validation + population of other fields based on the response's values in validator function.

So basically, I have three fields, one of them is the id. When the user enters an ID, it sends a request to the server to check if such id exists, if yes, it uses the response to populate two other fields. This behaviour seems pretty common in modern web, however I couldn't find an elegant way to implement this with final-form.

What I did seems a hacky solution for me. I stored form's initial state in the component's state and passed setState function to the validator. There if such id exists, I call setState with the response I got and then the form re-renders with updated values.

Any help much appreciated 🙂

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.