-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
v6: Form Submit : startSubmit action delayed due to asyncValidate #888
Comments
I'm sorry, what's the question? You think that |
Yes, because if we look at async validation example, the submit button when clicked its not disabling the submit button immediately, and giving user a chance to submit more than once. |
@erikras Yes i do agree , allowing multiple submit to user will be wrong thing to do |
Duplicates #829 |
I think this will do the trick I'm bit confused as to why is And if a user submits the form while async validation is in process, why is the form allowed to be submitted, can we not make form invalid so that from submit action will have no effect, since |
This is the correct answer. To answer your other questions, |
@erikras but while doing this on our |
@mihirsoni I've seen that, too. The two things are related, I think. |
@erikras i've tried to implement it using a promise counter which will keep track of async promises in progress, and as when the promises are resolved they will decrement the counter and during submit call we can check the File : reduxForm.js class Form extends Component {
constructor(props) {
super(props)
this.submit = this.submit.bind(this)
this.asyncValidate = this.asyncValidate.bind(this)
this.register = this.register.bind(this)
this.unregister = this.unregister.bind(this)
this.fields = {}
//add a asyncResolved counter
this.asyncResolved = 0
}
asyncValidate(name, value) {
const { asyncBlurFields, asyncValidate, dispatch, initialized, pristine, startAsyncValidation, stopAsyncValidation, syncErrors, values } = this.props
const isSubmitting = !name
if (asyncValidate) {
const valuesToValidate = isSubmitting ? values : setIn(values, name, value)
const syncValidationPasses = isSubmitting || !getIn(syncErrors, name)
const isBlurField = !isSubmitting &&
(!asyncBlurFields || ~asyncBlurFields.indexOf(name.replace(/\[[0-9]+\]/g, '[]')))
// if blur validating, only run async validate if sync validation passes and either no
// blur fields are passed or the field that has blurred is listed
// if submitting (not blur validation) or form is dirty or form was never initialized
if (syncValidationPasses && (isSubmitting || !pristine || !initialized) && (isSubmitting || isBlurField)) {
//increment the counter with each request
this.asyncResolved = ++this.asyncResolved
let asyncPromise = asyncValidation(
() => asyncValidate(valuesToValidate, dispatch, this.props),
startAsyncValidation,
stopAsyncValidation,
name
)
//when the promise is resolved or rejected decrement the counter
asyncPromise.then(()=> --this.asyncResolved,()=> --this.asyncResolved)
return asyncPromise
}
}
}
submit(submitOrEvent) {
//check if asyncResolved is in progress
if(this.asyncResolved!=0) {
console.log('Async call is in progress') // can throw an error or an event not sure.
submitOrEvent.preventDefault()
return
}
const { onSubmit } = this.props
const check = submit => {
if (!submit || typeof submit !== 'function') {
throw new Error(`You must either pass handleSubmit() an onSubmit function or pass onSubmit as a prop`)
}
return submit
}
return !submitOrEvent || silenceEvent(submitOrEvent) ?
// submitOrEvent is an event: fire submit
handleSubmit(check(onSubmit), this.props, this.valid, this.asyncValidate, this.fieldList) :
// submitOrEvent is the submit function: return deferred submit thunk
silenceEvents(() => handleSubmit(check(submitOrEvent), this.props, this.valid, this.asyncValidate, this.fieldList))
}
} |
Published fix as |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
@erikras
During form submission, we fire up
asyncValidate
and when the async promise is resolved it calls thedoSubmit
action with in turn calls thestartSubmit
, due to which thesubmitting
props received to the form is trigged lately, can we fire up thestartSubmit
as soon as the formssubmitHandler
is called.The text was updated successfully, but these errors were encountered: