Get 2024 Templates Mega Bundle!$1000 worth of 19 Bootstrap HTML, Vue & React Templates + 3 Vector Sets for just $99
Get for 99$

How to empty all the fields after form submission in vue


I am using composition api and i have used fields as reactive

<Field
type="text"
class="form-control form-control-lg "
name="product_name"
placeholder="Product Name"
v-model="productDetails.product_name">
</Field>

<div class="fv-plugins-message-container">
<div class="fv-help-block">
<ErrorMessage name="product_name"/>
</div>
</div>



const productDetails = reactive({
product_name : "",
});


I want to empty the fields after the submission as they were when we first open the component, How to empty all the fields ?


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (7)


Hi,

You can use resetForm function, and as a parameter provide an object of form initial values.

resetForm({
values: {
//your form initial values
},
});


You can find more form resetting options in official Vee-validate doc: https://vee-validate.logaretm.com/v4/guide/components/handling-forms

Regards,
Lauris Stepanovs,
Keenthemes Support Team



Hi Lauris ,
the above mentioned solution worked for the normal fields however it did not work for those fields which have v-slot in it ,


<Field name="product_type_id" v-slot="{ field,handleChange }">
<Multiselect v-model="productDetails.product_types.value"
v-bind="productDetails.product_types"
placeholder="Select Product Type"
@change="handleChange"
></Multiselect>
</Field>




or for textarea like

<Field
name="product_description"
v-model="productDetails.product_description"
v-slot="{ field,handleChange }"
>
<textarea class="form-control" placeholder="Description" @change="handleChange"></textarea>
</Field>



Hi,

Could you please try to remove v-model directive from your multiselect field?

And you can replace your textarea code with the same Field component with as="textarea" prop attribute.

<Field as="textarea" name="description" />


Regards,
Lauris Stepanovs,
Keenthemes Support Team



Hi Lauris,

The solution you told me for text-area worked however, even after removing the v-model from the multi-select it did not work,


<Field name="product_type_id" v-slot="{ field,handleChange }">
<Multiselect
v-bind="productDetails.product_types"
placeholder="Select Product Type"
@change="handleChange"
></Multiselect>
</Field>


<img file:///C:/Users/iba/Pictures/Screenshots/Screenshot%20(202).png>



Hi,

You need to remove handleChange function and @change event from your component, and add v-bind and v-model directives as shown below.


<Field name="yourFieldName" :value="defaultValue" v-slot="{ field }">
<Multiselect
v-bind="field"
v-model="field.value"
:options="options"
></Multiselect>
</Field>


Regards,
Lauris Stepanovs,
Keenthemes Support Team



It worked , Thanks a lot.



Hi,

Glad to hear that. Please don't hesitate to reach out if you need anything more from us.

Regards,
Lauris Stepanovs,
Keenthemes Support Team


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(