Skip to content

Commit 5109100

Browse files
committed
docs: update readme
1 parent 4c9bfd9 commit 5109100

File tree

2 files changed

+102
-60
lines changed

2 files changed

+102
-60
lines changed

README.md

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,55 @@ The main v4 version supports Vue 3.x only, for previous versions of Vue, check t
7575

7676
vee-validate offers two styles to integrate form validation into your Vue.js apps.
7777

78+
#### Composition API
79+
80+
If you want more fine grained control, you can use `useField` function to compose validation logic into your component:
81+
82+
```vue
83+
<script setup>
84+
// MyInputComponent.vue
85+
import { useField } from 'vee-validate';
86+
87+
const props = defineProps<{
88+
name: string;
89+
}>();
90+
91+
// Validator function
92+
const isRequired = value => (value ? true : 'This field is required');
93+
94+
const { value, errorMessage } = useField(props.name, isRequired);
95+
</script>
96+
97+
<template>
98+
<input v-model="value" />
99+
<span>{{ errorMessage }}</span>
100+
</template>
101+
```
102+
103+
Then you can you can use `useForm` to make your form component automatically pick up your input fields declared with `useField` and manage them:
104+
105+
```vue
106+
<script setup>
107+
import { useForm } from 'vee-validate';
108+
import MyInputComponent from '@/components/MyInputComponent.vue';
109+
110+
const { handleSubmit } = useForm();
111+
112+
const onSubmit = handleSubmit(values => {
113+
// Submit to API
114+
console.log(values); // { email: '[email protected]' }
115+
});
116+
</script>
117+
118+
<template>
119+
<form @submit="onSubmit">
120+
<MyInputComponent name="email" />
121+
</form>
122+
</template>
123+
```
124+
125+
You can do so much more than this, for more info [check the composition API documentation](https://vee-validate.logaretm.com/v4/guide/composition-api/validation/).
126+
78127
#### Declarative Components
79128

80129
Higher-order components are better suited for most of your cases. Register the `Field` and `Form` components and create a simple `required` validator:
@@ -99,42 +148,14 @@ Then use the `Form` and `Field` components to render your form:
99148

100149
```vue
101150
<Form v-slot="{ errors }">
102-
<Field name="field" :rules="isRequired" />
151+
<Field name="email" :rules="isRequired" />
103152
104-
<span>{{ errors.field }}</span>
153+
<span>{{ errors.email }}</span>
105154
</Form>
106155
```
107156

108157
The `Field` component renders an `input` of type `text` by default but you can [control that](https://vee-validate.logaretm.com/v4/api/field#rendering-fields)
109158

110-
#### Composition API
111-
112-
If you want more fine grained control, you can use `useField` function to compose validation logic into your component:
113-
114-
```js
115-
import { useField } from 'vee-validate';
116-
117-
export default {
118-
setup() {
119-
// Validator function
120-
const isRequired = value => (value ? true : 'This field is required');
121-
const { value, errorMessage } = useField('field', isRequired);
122-
123-
return {
124-
value,
125-
errorMessage,
126-
};
127-
},
128-
};
129-
```
130-
131-
Then in your template, use `v-model` to bind the `value` to your input and display the errors using `errorMessage`:
132-
133-
```vue
134-
<input name="field" v-model="value" />
135-
<span>{{ errorMessage }}</span>
136-
```
137-
138159
## 📚 Documentation
139160

140161
Read the [documentation and demos](https://vee-validate.logaretm.com/v4).

packages/vee-validate/README.md

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,55 @@ The main v4 version supports Vue 3.x only, for previous versions of Vue, check t
7575

7676
vee-validate offers two styles to integrate form validation into your Vue.js apps.
7777

78+
#### Composition API
79+
80+
If you want more fine grained control, you can use `useField` function to compose validation logic into your component:
81+
82+
```vue
83+
<script setup>
84+
// MyInputComponent.vue
85+
import { useField } from 'vee-validate';
86+
87+
const props = defineProps<{
88+
name: string;
89+
}>();
90+
91+
// Validator function
92+
const isRequired = value => (value ? true : 'This field is required');
93+
94+
const { value, errorMessage } = useField(props.name, isRequired);
95+
</script>
96+
97+
<template>
98+
<input v-model="value" />
99+
<span>{{ errorMessage }}</span>
100+
</template>
101+
```
102+
103+
Then you can you can use `useForm` to make your form component automatically pick up your input fields declared with `useField` and manage them:
104+
105+
```vue
106+
<script setup>
107+
import { useForm } from 'vee-validate';
108+
import MyInputComponent from '@/components/MyInputComponent.vue';
109+
110+
const { handleSubmit } = useForm();
111+
112+
const onSubmit = handleSubmit(values => {
113+
// Submit to API
114+
console.log(values); // { email: '[email protected]' }
115+
});
116+
</script>
117+
118+
<template>
119+
<form @submit="onSubmit">
120+
<MyInputComponent name="email" />
121+
</form>
122+
</template>
123+
```
124+
125+
You can do so much more than this, for more info [check the composition API documentation](https://vee-validate.logaretm.com/v4/guide/composition-api/validation/).
126+
78127
#### Declarative Components
79128

80129
Higher-order components are better suited for most of your cases. Register the `Field` and `Form` components and create a simple `required` validator:
@@ -99,42 +148,14 @@ Then use the `Form` and `Field` components to render your form:
99148

100149
```vue
101150
<Form v-slot="{ errors }">
102-
<Field name="field" :rules="isRequired" />
151+
<Field name="email" :rules="isRequired" />
103152
104-
<span>{{ errors.field }}</span>
153+
<span>{{ errors.email }}</span>
105154
</Form>
106155
```
107156

108157
The `Field` component renders an `input` of type `text` by default but you can [control that](https://vee-validate.logaretm.com/v4/api/field#rendering-fields)
109158

110-
#### Composition API
111-
112-
If you want more fine grained control, you can use `useField` function to compose validation logic into your component:
113-
114-
```js
115-
import { useField } from 'vee-validate';
116-
117-
export default {
118-
setup() {
119-
// Validator function
120-
const isRequired = value => (value ? true : 'This field is required');
121-
const { value, errorMessage } = useField('field', isRequired);
122-
123-
return {
124-
value,
125-
errorMessage,
126-
};
127-
},
128-
};
129-
```
130-
131-
Then in your template, use `v-model` to bind the `value` to your input and display the errors using `errorMessage`:
132-
133-
```vue
134-
<input name="field" v-model="value" />
135-
<span>{{ errorMessage }}</span>
136-
```
137-
138159
## 📚 Documentation
139160

140161
Read the [documentation and demos](https://vee-validate.logaretm.com/v4).

0 commit comments

Comments
 (0)