Written by: Geoffrey Callaghan

Easy Forms for VueJS

Easy Forms For Vuejs

Creating forms in Vue.js can be streamlined with libraries like fabform and other easy-to-use form libraries. Here’s a guide on how to work with fabform and other tools for managing forms in Vue.js.

1. Introduction to fabform

fabform is a lightweight and flexible form library for Vue.js that simplifies form handling, validation, and submission.

Installation

To get started, you need to install fabform:

npm install @fabform/vue

Basic Usage

  1. Import and Register: Import fabform in your Vue component or main.js file.
import { FabForm, FabField } from '@fabform/vue';

// If using in a single component
export default {
  components: {
    FabForm,
    FabField
  }
}
  1. Define a Form: Use <fab-form> and <fab-field> to define your form and fields.
<template>
  <fab-form @submit="handleSubmit">
    <fab-field name="email" label="Email" type="email" v-model="formData.email" required />
    <fab-field name="password" label="Password" type="password" v-model="formData.password" required />

    <button type="submit">Submit</button>
  </fab-form>
</template>

<script>
export default {
  data() {
    return {
      formData: {
        email: '',
        password: ''
      }
    };
  },
  methods: {
    handleSubmit(formData) {
      console.log('Form Submitted:', formData);
    }
  }
}
</script>

2. Validation

fabform provides built-in validation support. You can use validation rules directly in the fab-field component.

<fab-field 
  name="email" 
  label="Email" 
  type="email" 
  v-model="formData.email" 
  required 
  :rules="['required', 'email']"
/>

3. Handling Errors

fabform automatically handles error messages based on the validation rules. Customize error messages as needed.

<fab-field 
  name="email" 
  label="Email" 
  type="email" 
  v-model="formData.email" 
  required 
  :rules="['required', 'email']" 
  :messages="{ required: 'Email is required', email: 'Enter a valid email' }"
/>

4. Submitting the Form

fabform handles form submission through the @submit event. Ensure you validate and process form data in the handler method.

<fab-form @submit="handleSubmit">
  <!-- form fields -->
</fab-form>

5. Other Libraries for Vue.js Forms

Apart from fabform, there are several other popular form libraries for Vue.js:

Vuelidate

Vuelidate is a simple, lightweight model-based validation for Vue.js.

  1. Installation:
npm install @vuelidate/core @vuelidate/validators
  1. Basic Usage:
import useVuelidate from '@vuelidate/core'
import { required, email } from '@vuelidate/validators'

export default {
  data() {
    return {
      formData: {
        email: '',
        password: ''
      }
    };
  },
  validations() {
    return {
      formData: {
        email: { required, email },
        password: { required }
      }
    };
  },
  setup() {
    return {
      v$: useVuelidate()
    };
  },
  methods: {
    handleSubmit() {
      this.v$.$touch();
      if (this.v$.$invalid) {
        // handle errors
      } else {
        // handle form submission
      }
    }
  }
}

Vue Formulate

Vue Formulate offers a comprehensive solution for forms, including validation, styling, and more.

  1. Installation:
npm install @braid/vue-formulate
  1. Basic Usage:
import VueFormulate from '@braid/vue-formulate'

Vue.use(VueFormulate)
  1. Define a Form:
<template>
  <FormulateForm @submit="handleSubmit">
    <FormulateInput name="email" label="Email" type="email" validation="required|email" />
    <FormulateInput name="password" label="Password" type="password" validation="required" />
    <button type="submit">Submit</button>
  </FormulateForm>
</template>

<script>
export default {
  methods: {
    handleSubmit(data) {
      console.log('Form Submitted:', data);
    }
  }
}
</script>

Conclusion

Using fabform or other form libraries like Vuelidate and Vue Formulate can greatly simplify form handling in Vue.js applications. Choose the one that best fits your project requirements and enjoy streamlined form development with Vue.js.