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

Manner of clearing form without triggering validations #40

Closed
DevanB opened this issue Mar 16, 2020 · 4 comments · Fixed by #85
Closed

Manner of clearing form without triggering validations #40

DevanB opened this issue Mar 16, 2020 · 4 comments · Fixed by #85
Labels
future Accepted and tracking for an upcoming release
Milestone

Comments

@DevanB
Copy link

DevanB commented Mar 16, 2020

Describe the new feature you'd like
While building a form recently with Formulate, I found myself clearing the input (a password reset form) after I received a successful response from the server. When I set the value to an empty string, I noticed that the validation rules are running again. Does Formulate expose some helpers or way to clear a value (or values) without triggering validations? I've attached a screenshot of the issue.

What percentage of vue-formulate users would benefit?
Unknown, but I assume others will ask how to clear inputs without triggering validations eventually.

Screen Shot 2020-03-16 at 4 58 52 PM

@DevanB DevanB added the feature request New feature or request label Mar 16, 2020
@justin-schroeder
Copy link
Member

justin-schroeder commented Mar 16, 2020

Actually, and kinda incredibly, this hasn't come up before. I suppose people usually redirect users on success or don’t reset the form? Seems like a totally valid use case though.

Current API

There's an easy "vue" way to do this, you can swap out the key prop which is a special reserved prop in vue, and will cause the component to-remount fresh.

<FormulateForm
  :key="keyValue"
  @submit="submit"
>
 ...
</FormulateForm>

...

<script>
export default {
  data() {
    return {
       'keyValue': 'foo'
    },
   methods: {
      submit () {
         this.keyValue = 'bar' // this will reset the form
      }
   }
}
</script>

Future API

As a developer working on this problem right now, what API would you like? My inclination would be to tap into the named forms and add a "reset" method. Here's a proposal, tell me what you think:

<template>
<FormulateForm
  name="forgot"
  @submit="resetPassword"
>
  <FormulateInput
    type="email"
    name="email"
    validation="required|email"
  />
  <FormulateInput
    type="submit"
  />
</FormulateForm>
</template>

<script>
export default {
  methods: {
    async resetPassword () {
      try {
        const res = await this.$axios.restPassword('/api/reset')
        alert('success')
        this.$formulate.reset('forgot')
      } catch (err) {
        this.$formulate.handle(err, 'forgot')
    }
  }
}
</script>

That feels fairly clean to me, but I'm open to suggestions.

@justin-schroeder justin-schroeder added the suggestions welcome Looking for suggestions on a proposal label Mar 16, 2020
@DevanB
Copy link
Author

DevanB commented Mar 16, 2020

Love that you proposed a solution in the interim!

I think your proposal for a future solution makes perfect sense. With another form library I've used extensively, the form (itself) had a reset method. This is a similar API and encourages others to use the full abilities of the named form (like $formulate.handle, which is a hidden gem).

@justin-schroeder justin-schroeder added this to the 2.3.0 milestone Mar 26, 2020
@justin-schroeder justin-schroeder added this to 2.3.0 in Roadmap Mar 26, 2020
@justin-schroeder justin-schroeder added future Accepted and tracking for an upcoming release and removed suggestions welcome Looking for suggestions on a proposal feature request New feature or request labels Mar 26, 2020
@joaquinwojcik
Copy link

Any updates on this?

@justin-schroeder
Copy link
Member

Still on the roadmap for v2.3 which is starting to receive significant attention. As of today you need to use the key mechanism described above or another work around to "reset" the form.

@justin-schroeder justin-schroeder linked a pull request May 16, 2020 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
future Accepted and tracking for an upcoming release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants