ConrabOpto / mst-query

Query library for mobx-state-tree
MIT License
110 stars 7 forks source link

Form validation #21

Closed benjaminkwokhuen closed 1 year ago

benjaminkwokhuen commented 2 years ago

Just wondering how can I use createMutation to achieve form validation. Ideally, I want to use the mutation model to store the field values, but I also want to be able to validate user input on change.

k-ode commented 2 years ago

Good question. I tend to always use some separate form validation library for this, so I'm not really sure.

But it should be possible to use volatile state for the form state, and views for validation. You could probably defer updating the request model to when you are ready to save the form.

benjaminkwokhuen commented 2 years ago

That's what I ended up doing, using react-hook-form and yup to do validation. However, the main issue with this approach is that I need to maintain 2 sets of model (one from yup object schema, one from createMutation model). It would be great if there's a way to just have one model define along with the validation and use it to take care of everything.

k-ode commented 1 year ago

Check out mst-validation :)