Skip to content

Commit 7e2093b

Browse files
committed
docs: update defineField info
1 parent 7f17829 commit 7e2093b

File tree

3 files changed

+103
-9
lines changed

3 files changed

+103
-9
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ vee-validate offers two styles to integrate form validation into your Vue.js app
7979

8080
The fastest way to create a form and manage its validation, behavior, and values is with the composition API.
8181

82-
Create your form with `useForm` and then use `defineInputBinds` to create your fields bindings and `handleSubmit` to use the values and send them to an API.
82+
Create your form with `useForm` and then use `defineField` to create your field model and props/attributes and `handleSubmit` to use the values and send them to an API.
8383

8484
```vue
8585
<script setup>
@@ -91,14 +91,14 @@ function required(value) {
9191
}
9292
9393
// Create the form
94-
const { defineInputBinds, handleSubmit, errors } = useForm({
94+
const { defineField, handleSubmit, errors } = useForm({
9595
validationSchema: {
9696
field: required,
9797
},
9898
});
9999
100100
// Define fields
101-
const field = defineInputBinds('field');
101+
const [field, fieldProps] = defineField('field');
102102
103103
// Submit handler
104104
const onSubmit = handleSubmit(values => {
@@ -109,7 +109,7 @@ const onSubmit = handleSubmit(values => {
109109
110110
<template>
111111
<form @submit="onSubmit">
112-
<input v-bind="field" />
112+
<input v-model="field" v-bind="fieldProps" />
113113
<span>{{ errors.field }}</span>
114114
115115
<button>Submit</button>

docs/src/pages/api/use-form.mdx

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ handleReset();
600600

601601
<DocTip>
602602

603-
This is deprecated, please use `defineInputBinds` or `defineComponentBinds` instead.
603+
This is deprecated, please use `defineField` instead.
604604

605605
</DocTip>
606606

@@ -636,6 +636,12 @@ const [email, password] = useFieldModel(['email', 'password']);
636636

637637
</CodeTitle>
638638

639+
<DocTip>
640+
641+
This is deprecated, please use `defineField` instead.
642+
643+
</DocTip>
644+
639645
This creates a bindable object for the specified field. The bindable object only works with native HTML input elements, for components use `defineComponentBinds` instead.
640646

641647
The `defineInputBinds` must be called in the `setup` function.
@@ -667,6 +673,12 @@ const password = defineInputBinds('password');
667673

668674
</CodeTitle>
669675

676+
<DocTip>
677+
678+
This is deprecated, please use `defineField` instead.
679+
680+
</DocTip>
681+
670682
This creates a bindable object for the specified field. The bindable object only works with components, for native HTML input elements use `defineInputBinds` instead.
671683

672684
The `defineComponentBinds` must be called in the `setup` function.
@@ -692,3 +704,85 @@ const email = defineComponentBinds('email');
692704
const password = defineComponentBinds('password');
693705
</script>
694706
```
707+
708+
<CodeTitle level="4">
709+
710+
`defineField`
711+
712+
</CodeTitle>
713+
714+
This function returns a model and a props/attributes pair.
715+
716+
The `defineField` must be called in the `setup` function.
717+
718+
`defineField` accepts a single field path, You can use either root field paths or nested paths like `some.user.path` with dot notation.
719+
720+
```vue
721+
<template>
722+
<input v-model="email" v-bind="emailProps" />
723+
<span>{{ errors.email }}</span>
724+
725+
<input v-model="password" v-bind="passwordProps" type="password" />
726+
<span>{{ errors.password }}</span>
727+
</template>
728+
729+
<script setup>
730+
import { useForm } from 'vee-validate';
731+
732+
const { errors, defineField } = useForm();
733+
734+
const [email, emailProps] = defineField('email');
735+
const [password, passwordProps] = defineField('password');
736+
</script>
737+
```
738+
739+
This function also works with custom components.
740+
741+
You can configure the props/attributes that are bound to the component by passing a second argument to `defineField`. You can use this configuration to adjust the `field` validation behavior and more with the individual elements you are using or the component library of your choice. Check the [UI library examples](/examples/ui-libraries) for live examples.
742+
743+
```ts
744+
const [field, props] = defineField('field', {
745+
// A getter that computes and adds any additional props to the component
746+
// It receives the current field state as an argument
747+
props(state) {
748+
// This is just an example, by default this is an empty object
749+
return {
750+
'aria-invalid': state.errors.length > 0 ? 'true' : 'false',
751+
};
752+
},
753+
// A label for the field, only used with global validation rules
754+
label: 'a label',
755+
// Validates when `blur` event is emitted from the element/component
756+
validateOnBlur: true,
757+
// Validates when `change` event is emitted from the element/component
758+
validateOnChange: true,
759+
// Validates when `input` event is emitted from the element/component
760+
validateOnInput: false,
761+
// Validates when the returned model value changes
762+
validateOnModelUpdate: true,
763+
});
764+
```
765+
766+
You can also have a lazy configuration by passing a function that returns the configuration object. The only difference is `props` is now a plain object instead of a getter function.
767+
768+
This means you can change any of the configuration based on the field state:
769+
770+
```ts
771+
const [field, props] = defineField('field', state => {
772+
return {
773+
// A getter that computes and adds any additional props to the component
774+
props: {
775+
// This is just an example, by default this is an empty object
776+
'aria-invalid': state.errors.length > 0 ? 'true' : 'false',
777+
},
778+
// Validates when `blur` event is emitted from the element/component
779+
validateOnBlur: true,
780+
// Validates when `change` event is emitted from the element/component
781+
validateOnChange: true,
782+
// Validates when `input` event is emitted from the element/component
783+
validateOnInput: false,
784+
// Validates when the returned model value changes
785+
validateOnModelUpdate: true,
786+
};
787+
});
788+
```

packages/vee-validate/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ vee-validate offers two styles to integrate form validation into your Vue.js app
7979

8080
The fastest way to create a form and manage its validation, behavior, and values is with the composition API.
8181

82-
Create your form with `useForm` and then use `defineInputBinds` to create your fields bindings and `handleSubmit` to use the values and send them to an API.
82+
Create your form with `useForm` and then use `defineField` to create your field model and props/attributes and `handleSubmit` to use the values and send them to an API.
8383

8484
```vue
8585
<script setup>
@@ -91,14 +91,14 @@ function required(value) {
9191
}
9292
9393
// Create the form
94-
const { defineInputBinds, handleSubmit, errors } = useForm({
94+
const { defineField, handleSubmit, errors } = useForm({
9595
validationSchema: {
9696
field: required,
9797
},
9898
});
9999
100100
// Define fields
101-
const field = defineInputBinds('field');
101+
const [field, fieldProps] = defineField('field');
102102
103103
// Submit handler
104104
const onSubmit = handleSubmit(values => {
@@ -109,7 +109,7 @@ const onSubmit = handleSubmit(values => {
109109
110110
<template>
111111
<form @submit="onSubmit">
112-
<input v-bind="field" />
112+
<input v-model="field" v-bind="fieldProps" />
113113
<span>{{ errors.field }}</span>
114114
115115
<button>Submit</button>

0 commit comments

Comments
 (0)