You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Then you can you can use `useForm` to make your form component automatically pick up your input fields declared with `useField` and manage them:
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.
104
83
105
84
```vue
106
85
<script setup>
107
86
import { useForm } from 'vee-validate';
108
-
import MyInputComponent from '@/components/MyInputComponent.vue';
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/).
120
+
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/getting-started/).
126
121
127
122
#### Declarative Components
128
123
129
-
Higher-order components are better suited for most of your cases. Register the `Field` and `Form` components and create a simple `required` validator:
124
+
Higher-order components can also be used to build forms. Register the `Field` and `Form` components and create a simple `required` validator:
130
125
131
-
```js
126
+
```vue
127
+
<script setup>
132
128
import { Field, Form } from 'vee-validate';
133
129
134
-
exportdefault {
135
-
components: {
136
-
Field,
137
-
Form,
138
-
},
139
-
methods: {
140
-
isRequired(value) {
141
-
return value ?true:'This field is required';
142
-
},
143
-
},
144
-
};
145
-
```
130
+
// Validation, or use `yup` or `zod`
131
+
function required(value) {
132
+
return value ? true : 'This field is required';
133
+
}
146
134
147
-
Then use the `Form` and `Field` components to render your form:
135
+
// Submit handler
136
+
function onSubmit(values) {
137
+
// Submit to API
138
+
console.log(values);
139
+
}
140
+
</script>
148
141
149
-
```vue
150
-
<Form v-slot="{ errors }">
151
-
<Field name="email" :rules="isRequired" />
142
+
<template>
143
+
<Form v-slot="{ errors }" @submit="onSubmit">
144
+
<Field name="field" :rules="required" />
152
145
153
-
<span>{{ errors.email }}</span>
154
-
</Form>
146
+
<span>{{ errors.field }}</span>
147
+
148
+
<button>Submit</button>
149
+
</Form>
150
+
</template>
155
151
```
156
152
157
153
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)
@@ -165,6 +161,7 @@ Read the [documentation and demos](https://vee-validate.logaretm.com/v4).
165
161
You are welcome to contribute to this project, but before you do, please make sure you read the [contribution guide](/CONTRIBUTING.md).
To add translations, you can manually edit the JSON translation files in `packages/i18n/src/locale`, use the [inlang](https://inlang.com/) online editor, or run `pnpm machine-translate` to add missing translations using AI from Inlang.
Then you can you can use `useForm` to make your form component automatically pick up your input fields declared with `useField` and manage them:
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.
104
83
105
84
```vue
106
85
<script setup>
107
86
import { useForm } from 'vee-validate';
108
-
import MyInputComponent from '@/components/MyInputComponent.vue';
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/).
120
+
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/getting-started/).
126
121
127
122
#### Declarative Components
128
123
129
-
Higher-order components are better suited for most of your cases. Register the `Field` and `Form` components and create a simple `required` validator:
124
+
Higher-order components can also be used to build forms. Register the `Field` and `Form` components and create a simple `required` validator:
130
125
131
-
```js
126
+
```vue
127
+
<script setup>
132
128
import { Field, Form } from 'vee-validate';
133
129
134
-
exportdefault {
135
-
components: {
136
-
Field,
137
-
Form,
138
-
},
139
-
methods: {
140
-
isRequired(value) {
141
-
return value ?true:'This field is required';
142
-
},
143
-
},
144
-
};
145
-
```
130
+
// Validation, or use `yup` or `zod`
131
+
function required(value) {
132
+
return value ? true : 'This field is required';
133
+
}
146
134
147
-
Then use the `Form` and `Field` components to render your form:
135
+
// Submit handler
136
+
function onSubmit(values) {
137
+
// Submit to API
138
+
console.log(values);
139
+
}
140
+
</script>
148
141
149
-
```vue
150
-
<Form v-slot="{ errors }">
151
-
<Field name="email" :rules="isRequired" />
142
+
<template>
143
+
<Form v-slot="{ errors }" @submit="onSubmit">
144
+
<Field name="field" :rules="required" />
152
145
153
-
<span>{{ errors.email }}</span>
154
-
</Form>
146
+
<span>{{ errors.field }}</span>
147
+
148
+
<button>Submit</button>
149
+
</Form>
150
+
</template>
155
151
```
156
152
157
153
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)
@@ -164,6 +160,12 @@ Read the [documentation and demos](https://vee-validate.logaretm.com/v4).
164
160
165
161
You are welcome to contribute to this project, but before you do, please make sure you read the [contribution guide](/CONTRIBUTING.md).
To add translations, you can manually edit the JSON translation files in `packages/i18n/src/locale`, use the [inlang](https://inlang.com/) online editor, or run `pnpm machine-translate` to add missing translations using AI from Inlang.
168
+
167
169
## Credits
168
170
169
171
- Inspired by Laravel's [validation syntax](https://laravel.com/docs/5.4/validation)
0 commit comments