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

Add "defaults" method to Form helper #1019

Merged
merged 5 commits into from Dec 23, 2021
Merged

Conversation

claudiodekker
Copy link
Member

This PR adds a "defaults" method to the Form Helper, allowing people to reset the default values without initializing the form. Do note that it does not reset the form, but only changes the defaults that are used when the form (or some of it's fields) are reset.

Signatures:

setDefaults()
setDefaults(field: string, value: string)
setDefaults(fields: object)

Here's some examples of how you can use this:

Vue 2 / Vue 3

export default {
  data() {
    return {
      form: this.$inertia.form({
        email: 'johndoe@example.com',
        password: 'secret',
        remember: false,
      }),
    }
  },
  methods: {
    submit() {
      this.form.post('/login', {
        onSuccess: () => {
          // Set the current values as the new defaults
          this.form.defaults()

          // Only change the email field
          this.form.defaults('email', 'foo@example.com')

          // Change the defaults to multiple fields
          this.form.defaults({
            email: 'bar@example.com',
            password: 'hunter2',
          })
        },
      })
    },
  },
}

React

const { post, setDefaults } = useForm({
  email: 'johndoe@example.com',
  password: 'secret',
  remember: true
});

post('/login', {
  onSuccess: () => {
    // Set the current values as the new defaults
    setDefaults()

    // Only change the email field
    setDefaults('email', 'foo@example.com')

    // Change the defaults to multiple fields
    setDefaults({
      email: 'bar@example.com',
      password: 'hunter2',
    })
  }
}

Svelte

const form = useForm({
  email: 'johndoe@example.com',
  password: 'secret',
  remember: false,
})

function login() {
  $form.post(route('login.store'), {
    onSuccess: () => {
      // Set the current values as the new defaults
      $form.defaults()

      // Only change the email field
      $form.defaults('email', 'foo@example.com')

      // Change the defaults to multiple fields
      $form.defaults({
        email: 'bar@example.com',
        password: 'hunter2',
      })
    }
  })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants