re-formality icon indicating copy to clipboard operation
re-formality copied to clipboard

Side effects when validation fails?

Open jchavarri opened this issue 6 years ago • 3 comments

I have this form that involves different screens, with routing between them etc. The state is global, and shared across screens (which are "dumb components").

I'm looking at re-formality as I think it could simplify a lot the current implementation (amazing work on the library!) but one thing I'm wondering is how to perform side effects on validation error.

In particular, I have to:

  • change the route
  • focus on the input that failed to validate

whenever validation fails.

I see there's only an onSubmit param to the useForm hook, would it be possible to add onFailedValidation as well? Or is there another way to implement this kind of feature with re-formality?

jchavarri avatar Sep 26 '19 14:09 jchavarri

I think what I'm suggesting if it'd make sense to return UpdateWithSideEffects here:

https://github.com/MinimaHQ/re-formality/blob/0bf641cdebfa2c544ee1711beff6873f040b669f/src/Formality__FormAsyncOnBlurWithId.re#L372-L377

so it becomes:

else {
  UpdateWithSideEffects({
    ...state,
    fields,
    status: FormStatus.Editing,
    submittedOnce: true,
  },
  ({state, dispatch}) =>
    state.input
    ->onFailedValidation,
  )
};

jchavarri avatar Sep 26 '19 16:09 jchavarri

@jchavarri Your suggestion makes sense to me 👍 Though I'd use a bit different name here b/c onFailedValidation might be confusing since this function would be called only when validation failed during submission attempt but not on every failed validation (e.g. on change or on blur). Something like onFailedSubmissionValidation is clearer but probably kinda long. Thoughts?

alex35mil avatar Sep 27 '19 08:09 alex35mil

I will update onSubmit signature so it receives result((output, cb), unit) instead of output, cb arguments, so it would be possible to invoke effects on Error().

alex35mil avatar May 19 '20 11:05 alex35mil