iusehooks / usetheform

React library for composing declarative forms, manage their state, handling their validation and much more.
https://iusehooks.github.io/usetheform/
MIT License
86 stars 33 forks source link

Submit event doesn't trigger async inputs validations #32

Closed eseceve closed 3 years ago

eseceve commented 3 years ago

Problem

Submit event doesn't trigger async validations of each input

I don't know if this its a feature or bug

Current Behavior

Just only runs sync validations of each input

Expected Behavior

Run sync and async validations of each input

Demo

https://codesandbox.io/s/usetheform-asyncvalidators-submit-blbto

iusehooks commented 3 years ago

Hi @eseceve,

In your example you applied 2 validators one is Sync and one is Async.

Async validators run only if all the Sync validations have passed, no otherwise.

Reasons why is this way is to avoid calling async functions if your form is not valid.

Hope it helped.

 <TextField
          label="Standard"
          name="standard"
          touched
          validators={[required]}
        />
        <TextAsyncField
          label="Filled"
          variant="filled"
          name="filled"
          validator={asyncRequired}
        />
eseceve commented 3 years ago

Async validators run only if all the Sync validations have passed, no otherwise.

Reasons why is this way is to avoid calling async functions if your form is not valid.

The problem that I see with this approach it's I can't use only async validations in my forms. I'll need to write an asyncValidation for the form too, not just for each input.

eseceve commented 3 years ago

I would like to run async validations of each input on submit if there aren't validated yet

iusehooks commented 3 years ago

Would you please describe me a case in which you need to run an async validator function if a sync fails? Thanks

eseceve commented 3 years ago

I don't know why I had assumed that I couldn't use validators andasyncValidator at the same time. I already understood how to do what I need.

iusehooks commented 3 years ago

Hi @eseceve,

You can apply both validations sync and async on the same input or different inputs.

Priority is given to sync, so if one sync functions fails the async validators will not be excuted.

If you like usetheform a github star will be appriciated.

Thanks.