Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Side effects when validation fails? #56

Open
jchavarri opened this issue Sep 26, 2019 · 3 comments
Open

Side effects when validation fails? #56

jchavarri opened this issue Sep 26, 2019 · 3 comments

Comments

@jchavarri
Copy link

jchavarri commented Sep 26, 2019

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
Copy link
Author

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 jchavarri changed the title Side effects on error? Side effects when validation fails? Sep 26, 2019
@alex35mil
Copy link
Member

@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
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants