Comments (14)
I want to make three submit buttons: "Save", "Save and return to list", "Save and create next". Is there some way to provide additional information to submit handler?
from final-form.
Ooh, BEFORE the validation.. yes, that makes sense...
from final-form.
Your other option would be to set the value in the form.
<button onClick={() => {
form.change('pill', 'red') // triggers sync validation
form.submit() // will not submit if has sync errors
}}>Take the red pill</button>
from final-form.
Are there any updates on this? I want to do the same thing as @xiety
from final-form.
any update ???
from final-form.
Interesting idea. My most popular StackOverflow answer is about this specific use case.
I like the .with()
syntax you propose.
from final-form.
I saw you StackOverflow answer but it does not cover different validation. It adds additional values after the validation. I want the values injected before the validation so it can be processed by the validate
function.
from final-form.
you are fixing things faster than i can comment... please reopen
from final-form.
Okay, so doing it before validation is extremely complicated and actually doesn't make sense. Sync validation doesn't run when the form is submitted, it runs when the form values change.
You can still do your checks, but with synchronous client-side submit validation. Your best solution is going to be something like this:
onSubmit = values => {
if (values.pill === 'red') {
// let's say this means firstName is required
if (!values.firstName) {
return { firstName: 'First name is required if you want to take the red pill' }
}
} else if (values.pill === 'blue') {
// let's say this means lastName is required
if (!values.lastName) {
return { lastName: 'Last name is required if you want to take the blue pill' }
}
}
return ajax.send(values)
}
...
form.submit({ pill: 'red' })
Make sense?
from final-form.
Yeah. That's why I've created the issue :) Those options are all valid but equally hacky.
There is no clear approach to do this. What I would like to trigger the sync validation one additional time when
form.submit.with({pill:'red'})()
// or
form.submit.withChange({pill:'red'})()
is called.
One could ague that adding {pill:'red'}
before the submit is actually a change event so the validation is triggered again.
from final-form.
We have a similar case where we have 2 of the buttons listed by @xiety
from final-form.
Hey guys, sharing the way I solved it.
// fist I defined a ref to set the action I want to trigger later
const actionRef = useRef<string>("")
const onSubmit = async (v: FormValues) => {
// default submit code ...
if (actionRef.current === "send_quote") {
// Now I can trigger my send_quote action
} else {
// Now I can trigger my default action
alert.success("Quote saved successfully")
}
}
<Form
initialValues={initialValues}
onSubmit={onSubmit}
>
{({ handleSubmit, submitting }) => (
<form
onSubmit={e => {
// sets the action as the main here
actionRef.current = "main"
handleSubmit(e)
}}
>
... fields here
<button
// note, this is a submit type button
type="submit"
// check here is the action is main, so we can set is-loading class to this button
className={`button ml-2 is-primary ${submitting &&
actionRef.current === "main" &&
"is-loading"}`}
>
<span className="icon">
<i className="fas fa-save" />
</span>
<span>Save Quote</span>
</button>
<button
// note, this is a normal type button
type="button"
// check here is the action is "send_quote", so we can set is-loading class to this button
className={`button ml-2 is-primary ${submitting &&
actionRef.current === "send_quote" &&
"is-loading"}`}
// call by myself the handleSubmit action
onClick={() => {
actionRef.current = "send_quote"
handleSubmit()
}}
>
</form>
</Form>
from final-form.
I have not checked but does event.submitter.name
or event.nativeEvent.submitter.name
work? Just add to your buttons different name
and check
from final-form.
@AlekseyMalakhov That does sounds like a reasonable idea, but how does one get access to the event in the onSubmit
callback? 🤔 I don't see it mentioned anywhere:
https://final-form.org/docs/final-form/types/Config#onsubmit
from final-form.
Related Issues (20)
- initialValue overrides state on update HOT 1
- How to unregisterField, or at least update the registration? HOT 2
- [Feature Request] Get last dirty state of unregistered field (when `destroyOnUnregister` is false) HOT 2
- Migrate to TypeScript
- keepDirtyOnReinitialize can't handle undefined value HOT 2
- Confusing documentation/messaging
- final-form ^4.20.3 introduced a breaking change for array field names (e.g. choices[]), which worked in v4.20.2 HOT 1
- Field subscription `modifiedSinceLastSubmit`, is defined as `false`, rather than `boolean`. HOT 2
- Issue on TypeScript FieldSubscription
- form state becomes dirty when initial value is empty, and user interacting without actually changing the value HOT 1
- Reacting to onSubmit Promise rejection HOT 1
- focus/change/blur is not a function HOT 5
- Feature: Reuse field states from already created instance HOT 5
- FormSpy components are rendering sooner than previously expected
- FieldArray record level async validation not working HOT 1
- Error with validation when registering array field and array item fields together HOT 1
- Usage of keyof in TypeScript types does not align with field names
- [RFC] TypeScript rewrite of a few Final Form packages HOT 2
- Form not dirty after removing items from FieldArray using final-form-arrays
- Can't register field named 'constructor' HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from final-form.