September 16, 2022

Easily Handle Forms Validation Laravel-Vue Models Plugin

vform is a small library for Vue 2/3 to help with forms and validation when using Laravel as a back end.

Provides a form instance to conveniently encapsulate your data and send it to your Laravel application via an HTTP request using axios.

vue form validation example, vuelidate, custom validation in vue js, primevue form validation, form validation in vuetify, vue forms, vue 3 form validation

How to make use of it:

Install & Download:

npm install axios vform

Basic Usage:

<template>
  <form @submit.prevent="login" @keydown="form.onKeydown($event)">
    <input v-model="form.username" type="text" name="username" placeholder="Username">
    <div v-if="form.errors.has('username')" v-html="form.errors.get('username')" />

    <input v-model="form.password" type="password" name="password" placeholder="Password">
    <div v-if="form.errors.has('password')" v-html="form.errors.get('password')" />

    <button type="submit" :disabled="form.busy">
      Log In
    </button>
  </form>
</template>

<script>
import Form from 'vform'

export default {
  data: () => ({
    form: new Form({
      username: '',
      password: ''
    })
  }),

  methods: {
    async login () {
      const response = await this.form.post('/api/login')
      // ...
    }
  }
}
</script>

Laravel Controller:

<?php

class LoginController extends Controller
{
    public function login(Request $request)
    {
        $this->validate($request, [
            'username' => 'required',
            'password' => 'required',
        ]);

        // ...
    }
}

Instance properties

/**
 * Indicates if the form is sent to the server.
 */
form.busy: boolean

/**
 * Indicates if the response form the server was successful.
 */
form.successful: boolean

/**
 * Indicates if the response form the server was successful for two seconds.
 */
form.recentlySuccessful: boolean

/**
 * The validation errors from the server.
 */
form.errors: Errors

/**
 * The upload progress object.
 */
form.progress: { total: number, loaded: number, percentage: number } | undefined

Instance methods.

/**
 * Submit the form data via an HTTP request.
 */
form.submit(method: string, url: string, config = {})
form.post|patch|put|delete|get(url: string, config = {})

/**
 * Clear the form errors.
 */
form.clear()

/**
 * Reset the form data.
 */
form.reset()

/**
 * Update the form data.
 */
form.update({ ... })

/**
 * Fill the form data.
 */
form.fill({ ... })

/**
 * Clear errors on keydown.
 */
form.onKeydown(event: KeyboardEvent)

Setting a custom Axios instance

const instance = axios.create({
   baseURL: 'https://some-domain.com/api/'
})

Form.axios = instance

Form Validation In Vue 2 And Laravel, Handle Laravel-Vue forms and validation with ease Plugin/Github, nuxt form validation


vue form validation example

See Demo And Download

Official Website(cretueusebiu): Click Here

This superior jQuery/javascript plugin is developed by cretueusebiu. For extra Advanced usage, please go to the official website.

Share

You may also like...

Leave a Reply

Your email address will not be published.