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

formatOnBlur: Also run on submit #470

Closed
ThiefMaster opened this issue May 15, 2019 · 7 comments · Fixed by #478
Closed

formatOnBlur: Also run on submit #470

ThiefMaster opened this issue May 15, 2019 · 7 comments · Fixed by #478

Comments

@ThiefMaster
Copy link
Contributor

I'm using formatOnBlur to trim strings. This works fine, but if someone submits by pressing the enter key without leaving the field first, then no blur event happens and the string is submitted as-is without the formatter code running.

Or is there a better way to trim all strings before submission? format+formatOnBlur is pretty convenient because we're using a custom Field wrapper component that always sets it for string fields.

@erikras
Copy link
Member

erikras commented May 15, 2019

You could always do the formatting in your submit function before sending it off to the server. Something like:

function onSubmit(values) {
  stringKeys.forEach(key => values[key] = format(values[key]))
  ajax.send(values)
}
<Form onSubmit={onSubmit}/>

@ThiefMaster
Copy link
Contributor Author

I hoped to avoid having to put code for this in every single submit function ;) Also, this wouldn't trigger validation errors for fields that cannot be empty or have a minimum length (which I want to apply on the trimmed value of course).

@erikras
Copy link
Member

erikras commented May 16, 2019

You make good points.

@erikras
Copy link
Member

erikras commented May 16, 2019

You could use jQuery! 🤣 I'm joking...

Because React Final Form is just responding to the events the browser gives it, this is going to require more complex communication between the Final Form engine and the fields.

@erikras
Copy link
Member

erikras commented May 16, 2019

Published fix in v5.1.0.

@lock
Copy link

lock bot commented May 20, 2020

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.

1 similar comment
@lock
Copy link

lock bot commented May 20, 2020

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.

@lock lock bot locked as resolved and limited conversation to collaborators May 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
2 participants