5 Proven Tips to Make Your Vue Forms Foolproof and Boost Development Speed
Learn how to streamline Vue form development by applying five practical techniques—including universal validation, preventing duplicate submissions, managing loading states, resetting data on reopen, and prompting users before accidental navigation—each illustrated with Element‑UI code snippets to reduce bugs, cut communication overhead, and accelerate project delivery.
1. Emphasize Generic Form Validation
Business scenario: Phone numbers need special regex validation, while generic fields like title and description are often overlooked.
Problem: Repeated defects in TAPD due to missing basic validation such as trimming spaces, max length, and disallowing special characters.
Solution:
Trim input values (v-model.trim).
Set maximum length (max-length).
Disallow special characters (e.g., emojis) using pattern validation.
<code>// form rules
export default {
title: [
// Tips: trim spaces automatically
{ max: 50, message: 'Cannot exceed 50 characters', trigger: 'blur' },
{
pattern: /^[\u0020-\u007E \u4E00-\u9FA5 \uFF00-\uFF61 \u2000-\u202F \u3000-\u3017]+$/,
message: 'Cannot contain special characters',
trigger: 'blur'
}
]
}
</code>2. Prevent Duplicate Submissions
Business scenario: Users may click the submit button repeatedly, causing multiple requests.
Problem: Duplicate requests waste time and generate many defects.
Solution 1: Block further submissions until the current request finishes.
<code>export default {
methods: {
onSubmit() {
if (this.isCommitting) return;
this.isCommitting = true;
// request logic …
this.isCommitting = false;
}
}
}
</code>Solution 2: Use debounce to limit click frequency.
<code>export default {
mounted() {
this.debouncedSaveForm = _.debounce(this.onSubmit, 500, { leading: true, trailing: false });
},
methods: {
onSubmit() {
if (this.isCommitting) return;
this.isCommitting = true;
// request logic …
this.isCommitting = false;
}
}
}
</code>3. Loading Indicators for Submit and Errors
Business scenario: No loading indicator during long requests, and loading remains visible on error.
Solution:
<code>export default {
methods: {
onSubmit() {
this.$loading.show('Loading...');
request('apiUrl', data)
.then(() => { /* success */ })
.catch(() => { /* error */ })
.finally(() => {
this.$loading.hide();
});
}
}
}
</code>4. Reset Form Data When Reopened
Business scenario: The same dialog is used for both adding and editing, causing stale data to appear.
Solution: Clone the original form data on mount, and restore it when the dialog opens for “add”. Use deep clone to avoid reference issues.
<code>export default {
mounted() {
this._bak_form = _.cloneDeep(this.form);
},
methods: {
onOpenDialog(actionType) {
if (actionType === 'add') {
this.form = _.cloneDeep(this._bak_form);
} else if (actionType === 'edit') {
this.form = _.cloneDeep(data);
}
this.actionType = actionType;
this.visible = true;
}
}
}
</code>5. Confirm Before Accidental Page Close
Business scenario: Users may accidentally close the page or navigate away while filling a long form.
Solution: Watch the dialog visibility and set
window.onbeforeunloadto prompt the user when there are unsaved changes.
<code>export default {
watch: {
visible(value) {
if (!value) {
window.onbeforeunload = null;
return;
}
window.onbeforeunload = e => {
e.returnValue = 'Are you sure you want to leave this page?';
};
}
}
}
</code>Applying these five techniques in Vue + Element‑UI projects can make form requirements pass on the first try, reduce back‑and‑forth communication, and speed up overall development.
Tencent IMWeb Frontend Team
IMWeb Frontend Community gathering frontend development enthusiasts. Follow us for refined live courses by top experts, cutting‑edge technical posts, and to sharpen your frontend skills.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.