Skip to content

General Form

A generic form component that provides consistent form layout, validation, and submission handling.

Usage

html
<general-form 
  :schema="formSchema"
  v-model="formData"
  @submit="handleSubmit"
  @cancel="handleCancel"
></general-form>

Props

PropTypeDefaultDescription
schemaFormSchemaRequiredSchema defining form fields and validation
modelValueanyRequiredForm data object (v-model)
loadingbooleanfalseLoading state for submit button
submitTextstring'Submit'Text for submit button
cancelTextstring'Cancel'Text for cancel button
showCancelbooleantrueWhether to show cancel button
layout'vertical' | 'horizontal''vertical'Form layout direction

Events

EventPayloadDescription
submitformDataEmitted when form is submitted with valid data
cancelnoneEmitted when cancel button is clicked
validation-errorerrorsEmitted when validation fails

Types

typescript
interface FormField {
  name: string
  label: string
  type: 'text' | 'number' | 'email' | 'password' | 'select' | 'textarea' | 'checkbox' | 'radio' | 'date'
  required?: boolean
  placeholder?: string
  options?: Array<{ label: string; value: any }>
  validation?: ValidationRule[]
  component?: string // Custom component name
  props?: Record<string, any> // Additional props for custom components
}

interface FormSchema {
  fields: FormField[]
  validation?: Record<string, ValidationRule[]>
}

Features

  • Dynamic Fields: Supports various input types and custom components
  • Validation: Built-in validation with custom rules
  • Flexible Layout: Vertical or horizontal form layouts
  • Loading States: Integrated loading state for async submissions

Examples

Basic Form

html
<script setup>
const formData = ref({
  name: '',
  email: '',
  role: 'user'
})

const formSchema = {
  fields: [
    {
      name: 'name',
      label: 'Full Name',
      type: 'text',
      required: true,
      placeholder: 'Enter your full name'
    },
    {
      name: 'email',
      label: 'Email Address',
      type: 'email',
      required: true,
      validation: [{ type: 'email', message: 'Invalid email format' }]
    },
    {
      name: 'role',
      label: 'Role',
      type: 'select',
      options: [
        { label: 'User', value: 'user' },
        { label: 'Admin', value: 'admin' }
      ]
    }
  ]
}
</script>

<template>
  <general-form 
    :schema="formSchema"
    v-model="formData"
    @submit="saveUser"
  />
</template>

With Custom Component

html
<script setup>
const formSchema = {
  fields: [
    {
      name: 'avatar',
      label: 'Profile Picture',
      component: 'avatar-uploader',
      props: { maxSize: 2048 }
    },
    {
      name: 'bio',
      label: 'Biography',
      type: 'textarea',
      placeholder: 'Tell us about yourself...'
    }
  ]
}
</script>

© Velocitech LTD. All rights reserved.

Build SaaS Products Without Limits.