S
S
SeniorScript2021-02-16 19:55:34
React
SeniorScript, 2021-02-16 19:55:34

How to properly validate a form?

Good day!
Such a task: when you click on the "Edit Post" button, you go to the form and the getPost action takes place, which takes the post id and returns it. I pull this post from the storage (redux) using useSelector and assign a defaultValue to each input (for example: defaultValue ={post.title} ), and also set a ref to each input using useRef.
When the user has edited a post, he presses the edit button, which calls the async method, a new object is created in this method and current.value is assigned to each property of this object (for example: let updatePost = {title: titleRef.current.value } and so on), then an action is called which takes this post and id, and updates the post.

All this works fine, but you need to do validation, how to do it right?
I tried to do it using Formik, everything works correctly when creating a new post, because the initial value is empty, and when editing a post I don’t know how to do it.

Tell me please

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
abberati, 2021-02-16
@abberati

https://formik.org/docs/api/formik#initialvalues-values

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question