Closed EfstathiadisDimitris closed 5 years ago
Hey! Thanks for the write up. How best to structure Create vs. Edit is a decision that comes up in pretty much every single application. Unfortunately, the answer is: it depends. The good news is that your reasoning about this problem is 100% on track. In general, I like to to keep things that are similar (i.e. likely to be changed at the same time) as close as possible and separate things that are different (i.e. unlikely to be changed at the same time). Some observations/guesses...
Cognizant of the above, I suggest actually making 3 forms. While yes, you can use the function version of validation schema, I find that this tends to get messy when you have more than 2 versions of your form. Since you are breaking out edit user info and edit password, I would thus split each of these up into totally separate <Formik>
's with their own validationSchema's (although you could in theory share the parts of the inner object). FWIW in our apps, this decision is very clear cut because we make a special update password endpoint (that's different from update user) that takes the old password and the new one (and confirms that they are indeed different values).
I hope that helps.
Side note: Very very clever to use Yup.oneOf()
for matching. In the past, we've just written out our own Yup.equalTo
as described in https://github.com/jquense/yup/issues/97
My question revolves around a particular scenario I am currently facing. I am using the
withFormik
H.O.C, along with Yup, to handle various cases in my forms, such as submitting, errorHandling, and a few more depending on the situation.Usually, my form situation involves a
Create
and anEdit
Mode. onCreate => Pass the defaultvalues and call thePOST
Method, from my services. onEdit => Populate the values with the current Item Values from the Server, and call thePUT
Method from the services.Example
That is actually been working great for me, since my Create and Edit Form are the same. And here lay my 2 problems.
Current Situation
My current form implementation has 2 views. One unified one on Create with those 4 fields, and on Edit, I have 2 forms. One for
passwordChange
and one forinfoChange
. Which makes me face the following problems.CREATE
,EDIT-INFO
,EDIT-PASSWORD
). Which I am not sure formik even supports.onSubmit
,ErrorMessage
for both field error and statusError?If you could actually help me out figure a way of attack it would be great.
I read the
validationSchema
can be passed a function that returns a validationSchema, so I did this, but it is not working:I probably am doing something wrong, but I put it here just in case.
My Opinion on the matter