GithubHelp home page GithubHelp logo

Comments (14)

xiety avatar xiety commented on September 27, 2024 23

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.

erikras avatar erikras commented on September 27, 2024 1

Ooh, BEFORE the validation.. yes, that makes sense...

from final-form.

erikras avatar erikras commented on September 27, 2024 1

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.

harunsmrkovic avatar harunsmrkovic commented on September 27, 2024 1

Are there any updates on this? I want to do the same thing as @xiety

from final-form.

hewenguang avatar hewenguang commented on September 27, 2024 1

any update ???

from final-form.

erikras avatar erikras commented on September 27, 2024

Interesting idea. My most popular StackOverflow answer is about this specific use case.

I like the .with() syntax you propose.

from final-form.

kromit avatar kromit commented on September 27, 2024

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.

kromit avatar kromit commented on September 27, 2024

you are fixing things faster than i can comment... please reopen

from final-form.

erikras avatar erikras commented on September 27, 2024

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.

kromit avatar kromit commented on September 27, 2024

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.

santaclauze avatar santaclauze commented on September 27, 2024

We have a similar case where we have 2 of the buttons listed by @xiety

from final-form.

manoelneto avatar manoelneto commented on September 27, 2024

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.

AlekseyMalakhov avatar AlekseyMalakhov commented on September 27, 2024

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.

isti115 avatar isti115 commented on September 27, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.