shakacode / re-formality

Form validation tool for reason-react
https://re-formality.now.sh
MIT License
244 stars 35 forks source link

Side effects when validation fails? #56

Open jchavarri opened 4 years ago

jchavarri commented 4 years ago

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:

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 commented 4 years ago

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,
  )
};
alex35mil commented 4 years ago

@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 commented 4 years ago

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().