Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Mar 20, 2020. It is now read-only.
/vue-use-formPublic archive

✅ A Vue.js composition API function to validate forms

License

NotificationsYou must be signed in to change notification settings

logaretm/vue-use-form

Repository files navigation

⚠ Deprecated. ⚠

This project will be continued in the future releases of vee-validate starting withvee-validate@4 which will come with Vue.js 3 support.


This is aVue composition API function that allows you to do form validation, powered by vee-validate.

Install

⚠ Not production ready yet. ⚠

yarn add vue-use-form# ORnpm i vue-use-form

Usage

In your component file:

import{ref}from'@vue/composition-api';import{useForm,useField}from'vue-use-form';exportdefault{setup(){constfname=ref('');const{ form, submit}=useForm({onSubmit(){console.log('Submitting!',{fname:fname.value});}});const{ errors}=useField('firstName',{rules:'required',value:fname,      form});return{ fname, errors, submit};}};

In your Template:

<form @submit.prevent="submit">  <input v-model="fname">  <span>{{ errors[0] }}</span>  <button>Submit</button></form>

API

useForm(object)

TheuseForm function accepts options to configure the form.

const{ form, submit}=useForm({onSubmit(){// this will only run when the form is valid.// send to server!}});

It returns an object containing two properties:

  • form: A form controller object, which can be used withuseField to associate fields in the same form.
  • submit: A safe form submit handler to bind to your submission handler, it will validate all the fields before it runs the givenonSubmit handler.

useField(string, string | object)

TheuseField function accepts astring which is the input name, and options to configure the field:

constfield=useField('firstName',{rules:'required',// vee-validate style rules, can be a Ref<string>.value:fname,// the initial field value, optional.  form// a form controller object returned from "useForm", used to group fields. optional.});

It returns the following members:

PropTypeDescription
flagsValidationFlagsObject containing vee-validate flags for that field.
errorsstring[]list of validation errors
validate() => ValidationResultTriggers validation for the field.
reset() => voidResets validation state for the field.
onInput() => voidUpdates some flags when the user inputs, you should bind it to the element.
onBlur() => voidUpdates some flags when the user focuses the field, you should bind it to the element.
valueRef<string>A default ref in-case you didn't pass the initial value

Credits

License

MIT

About

✅ A Vue.js composition API function to validate forms

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp