score:2

Accepted answer

assuming that the response from your server contains the errors you can set the errors for the fields using the setfielderror function.

the code below assumes that the server returns an error status code with a response that contains the errors in the follow format [{"field":"name","message":"name cannot be empty."}].

const handlesubmit = (values, actions) => {
  makesomepostrequest(values)
    .then(response => console.log('success!'))
    .catch(error => {
      /* if your server returns something like a 422 when the
         validation fails it will move to the catch part. here
         you can use the actions.setfielderror to set the errors
         in your form.
      */
      error.response.data.foreach(error => {
        actions.setfielderror(error.field, error.message)
      })
    })
    .finally(() => actions.setsubmitting(false))
}

Related Query

More Query from same tag