Super Sale Limited Time 50% OFF for All-Access Plans
Save 50% Now

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
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • 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:

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
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(