Logo

dev-resources.site

for different kinds of informations.

Vue 3 Forms and Validations with VueFormify and yup

Published at
5/9/2024
Categories
vue
forms
validations
yup
Author
mateenagy
Categories
4 categories in total
vue
open
forms
open
validations
open
yup
open
Author
9 person written this
mateenagy
open
Vue 3 Forms and Validations with VueFormify and yup

What is VueFormify?
VueFormify is a lightweight form building package I working on since last year. It is not a UI library although it contains some basic inputs(without styles) and you can easily create custom inputs.

It also have wrapper for several UI libraries and schema validators which can be added by package managers.

Some feature:

  • Auto collect values
  • Nested Objects and Arrays
  • Form level validation with the most popular schema validators
  • Easy to integrate
  • Lightweight (~3kb gzipped)

Let’s make our first form!
We will create a simple login form with yup schema validator. With yup we can easily define nested object rules which make client-side validation much more easier. Check yup documentation for more details.

First we need to install the required packages:

npm i vue-formify yup @vue-formify/yup
Enter fullscreen mode Exit fullscreen mode

Because VueFormify build to support multiple schema validators we need to install @vue-formify/yup which contains the wrapper for yup schema object.

import { FormifyForm, FormifyError, FormifyInput } from 'vue-formify';
import { schemaFromYup } from '@vue-formify/yup';
import * as yup from 'yup';
Enter fullscreen mode Exit fullscreen mode

After we install and import the required packages we can define our rules and create our function which handle our submit event:

<script setup lang="ts">
import { FormifyForm, FormifyError, FormifyInput } from 'vue-formify';
import { schemaFromYup } from '@vue-formify/yup';
import * as yup from 'yup';

const schema = schemaFromYup(
  yup.object({
    email: yup.string().email('Invalid email').required('Required field'),
    password: yup.string().required('Required field'),
  })
);

const sendForm = (data) => {
  console.log('data: ', data);
};

</script>
Enter fullscreen mode Exit fullscreen mode

Our next step is to create the form itself.

The form automatically extract the data:

<template>
  <FormifyForm :validation-schema="schema" @submit="sendForm">
    <FormifyInput name="email" />
    <FormifyInput name="password" />

    <button type="submit">Submit</button>
  </FormifyForm>
</template>
Enter fullscreen mode Exit fullscreen mode

Here is the full code:

<script setup lang="ts">
import { FormifyForm, FormifyInput } from 'vue-formify';
import { schemaFromYup } from '@vue-formify/yup';
import * as yup from 'yup';

const schema = schemaFromYup(
  yup.object({
    email: yup.string().email('Invalid email').required('Required field'),
    password: yup.string().required('Required field'),
  })
);

const sendForm = (data) => {
  console.log('data: ', data);
};
</script>
<template>
  <FormifyForm :validation-schema="schema" @submit="sendForm">
    <FormifyInput name="email" />
    <FormifyInput name="password" />

    <button type="submit">Submit</button>
  </FormifyForm>
</template>
Enter fullscreen mode Exit fullscreen mode

Here is a stackblitz where you can play around:

This is a very basic example and in the future I will make other post about using more advanced forms with different schema validators and UI libraries.

If you find this helpful please leave a comment or a give a ⭐️ on VueFormify github page. I really appreciate any feedback.

forms Article's
30 articles in total
Favicon
Shared form with config files in NextJs
Favicon
Building a Wedding Website with Next.js, Supabase, and Tailwind CSS
Favicon
Mastering Form Error Handling in Angular: Mapping Errors to User-Friendly Messages
Favicon
React: Optimizing Forms with Controlled and Uncontrolled Components
Favicon
How To Solve The Problem of the Vanishing PDF Form Data
Favicon
A Web Component for Conditional Dependent Fields
Favicon
Angular Typed Forms: Precision and Power in Form Management
Favicon
Requirement Rules for Checkboxes
Favicon
Using forms to create AI tools
Favicon
React-Hook-Form vs Formik: The Good, Bad, and Ugly
Favicon
Angular Form Architecture: Achieving Separation of Concerns with a FormHandlerService
Favicon
Simplifying Error Handling in Angular Forms with a Custom Directive
Favicon
Inaccessible forms
Favicon
Vue 3 Forms and Validations with VueFormify and yup
Favicon
Embed a form builder with Swift
Favicon
Uploading Files with React (POST Request)
Favicon
Easy Forms for VueJS with Formspree
Favicon
Guide to building fillable forms into your app
Favicon
Embed a form builder with Angular
Favicon
Angular Reactive Forms Basics
Favicon
Integrating Yii3 packages into WordPress
Favicon
Reactive Validation Triggers in Angular Forms
Favicon
Embed a form builder with React
Favicon
List of useful tools & widgets for website to boost marketing
Favicon
Embed a form builder with Javascript
Favicon
Building a Dynamic Contact Form for Your Hugo Static Website
Favicon
Exploring the Pros and Cons of AuraQuantic
Favicon
Why you should stop using google forms in 2024?
Favicon
How to create static website form with FabForm
Favicon
How to html forms

Featured ones: