Skip to content

Commit 2ce568a

Browse files
committed
docs: more doc updates
1 parent d08bdfa commit 2ce568a

File tree

4 files changed

+73
-221
lines changed

4 files changed

+73
-221
lines changed

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

Lines changed: 1 addition & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -92,89 +92,6 @@ useField('password', yup.string().required().min(8));
9292

9393
## API Reference
9494

95-
### TypeScript Definition
96-
97-
The full signature of the `useField` function looks like this:
98-
99-
```ts
100-
interface FieldOptions<TValue = unknown> {
101-
validateOnValueUpdate?: boolean; // if the field should be validated when the value changes (default is true)
102-
initialValue?: MaybeRef<TValue>;
103-
validateOnMount?: boolean; ; // if the field should be validated when the component is mounted
104-
bails?: boolean; // if the field validation should run all validations
105-
label?: MaybeRef<string | undefined>; // A friendly name to be used in `generateMessage` config instead of the field name, has no effect with yup rules
106-
standalone?: boolean; // // Excludes the field from participating in any `Form` or `useForm` contexts, useful for creating inputs that do contribute to the `values`, In other words, the form won't pick up or validate fields marked as standalone
107-
type?: string; // can be either `checkbox` or `radio` or `file` any other types doesn't have an effect.
108-
109-
// Both of these are only used if `type="checkbox"`. They are ignored otherwise
110-
checkedValue?: MaybeRef<TValue>; // If a checkbox this will be used as the new field value when it is checked.
111-
uncheckedValue?: MaybeRef<TValue>; // If a single checkbox this will be used as the field value when it is unchecked.
112-
113-
keepValueOnUnmount?: boolean; // if the form value should be kept when the field is unmounted, default is `false`
114-
modelPropName?: string; // the model prop name, default is `modelValue`
115-
syncVModel?: boolean; // If the model prop should be synced with value changes for `v-model` support, default is `true`
116-
}
117-
118-
interface ValidationResult {
119-
errors: string[];
120-
valid: boolean;
121-
}
122-
123-
interface FieldState<TValue = unknown> {
124-
value: TValue;
125-
dirty: boolean;
126-
touched: boolean;
127-
errors: string[];
128-
}
129-
130-
export interface FieldMeta<TValue> {
131-
touched: boolean; // field was blurred or attempted submit
132-
dirty: boolean; // field value was changed
133-
valid: boolean; // field doesn't have any errors
134-
validated: boolean; // field was validated via user input
135-
pending: boolean; // field is pending validation
136-
initialValue: TValue | undefined; // initial field value
137-
}
138-
139-
/**
140-
* validated-only: only mutate the previously validated fields
141-
* silent: do not mutate any field
142-
* force: validate all fields and mutate their state
143-
*/
144-
export type SchemaValidationMode = 'validated-only' | 'silent' | 'force';
145-
146-
export interface ValidationOptions {
147-
mode: SchemaValidationMode;
148-
}
149-
150-
function useField<TValue = unknown>(
151-
fieldName: MaybeRef<string>,
152-
rules?: MaybeRef<RuleExpression>,
153-
opts?: FieldOptions
154-
): {
155-
name: MaybeRef<string>; // The field name
156-
value: Ref<TValue>; // the field's current value
157-
meta: FieldMeta<TValue>;
158-
errors: Ref<string[]>; // all error messages
159-
errorMessage: Ref<string | undefined>; // the first error message
160-
label?: MaybeRef<string | undefined>;
161-
type?: string;
162-
bails?: boolean;
163-
checkedValue?: MaybeRef<TValue>;
164-
uncheckedValue?: MaybeRef<TValue>;
165-
checked?: Ref<boolean>; // Present if input type is checkbox
166-
resetField(state?: Partial<FieldState<TValue>>): void; // resets errors and field meta, updates the current value to its initial value
167-
handleReset(): void;
168-
validate(opts?: Partial<ValidationOptions>): Promise<ValidationResult>; // validates and updates the errors and field meta
169-
handleChange(e: Event | unknown, shouldValidate?: boolean): void; // updates the value and triggers validation
170-
handleBlur(e?: Event): void; // updates the field meta associated with blur event
171-
setState(state: Partial<FieldState<TValue>>): void;
172-
setTouched(isTouched: boolean): void;
173-
setErrors(message: string | string[]): void;
174-
setValue(value: TValue): void;
175-
};
176-
```
177-
17895
### Composable API
17996

18097
The following sections documents each available property on the `useField` composable.
@@ -313,7 +230,7 @@ Note that it is unsafe to use this function as an event handler directly, check
313230
```
314231

315232
You can also use `handleReset` which is a safe alternative for `resetField`.
316-
233+
317234
<CodeTitle level="4">
318235

319236
`setErrors: (errors: string | string[]) => void`

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

Lines changed: 72 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,6 @@ import DocBadge from '@/components/DocBadge.vue';
1414

1515
`useForm` is a custom composition API function that allows you to group fields created by `useField` and aggregates their state. It should be used to create logical forms or custom form components similar to the `<Form/>` component which is just a consumer of `useForm`.
1616

17-
The most basic usage is where you have both `useField` and `useForm` at the same setup function:
18-
19-
```js
20-
import { useField, useForm } from 'vee-validate';
21-
import * as yup from 'yup';
22-
23-
export default {
24-
setup() {
25-
useForm();
26-
const { value: email, errors } = useField('email', yup.string().email().required());
27-
28-
return {
29-
email,
30-
errors,
31-
};
32-
},
33-
};
34-
```
35-
3617
## Field Types
3718

3819
The `useForm` function has field typing capabilities if you need it, getting type information for your fields and their values can be very powerful when building complex forms.
@@ -64,6 +45,8 @@ const { errors, setErrors, setFieldValue } = useForm({
6445
});
6546
```
6647

48+
Alternatively, you can use a [typed schema](/guide/composition-api/typed-schema/) to infer the form types from the validation schema if you are using yup or zod.
49+
6750
Whichever approach you prefer, you get full type information for your fields for all the functions exposed by `useForm`, here are a few examples.
6851

6952
```ts
@@ -97,114 +80,6 @@ It will error out because `age` is not defined in the `LoginForm` type you defin
9780

9881
## API Reference
9982

100-
### TypeScript Definition
101-
102-
The full signature of the `useForm` function looks like this:
103-
104-
```ts
105-
interface FormOptions<TValues extends Record<string, any>> {
106-
validationSchema?: any; // A yup schema, or a Record<string, any> containing valid rules as `useField`
107-
initialValues?: MaybeRef<TValues>;
108-
initialErrors?: Record<keyof TValues, string | undefined>; // a map of the form's initial error messages
109-
initialTouched?: Record<keyof TValues, boolean>; // a map of the form's initial touched fields
110-
validateOnMount?: boolean;
111-
keepValuesOnUnmount?: boolean; // if it should remove the fields that get unmounted value from the form values object, default is `false`
112-
}
113-
114-
/**
115-
* validated-only: only mutate the previously validated fields
116-
* silent: do not mutate any field
117-
* force: validate all fields and mutate their state
118-
*/
119-
type SchemaValidationMode = 'validated-only' | 'silent' | 'force';
120-
121-
interface ValidationOptions {
122-
mode: SchemaValidationMode;
123-
}
124-
125-
type MapValues<T, TValues extends Record<string, any>> = {
126-
[K in keyof T]: T[K] extends MaybeRef<infer TKey>
127-
? TKey extends keyof TValues
128-
? Ref<TValues[TKey]>
129-
: Ref<unknown>
130-
: Ref<unknown>;
131-
};
132-
133-
interface FormActions<TValues = Record<string, any>> {
134-
setFieldValue<T extends keyof TValues>(field: T, value: TValues[T], opts?: Partial<SetFieldValueOptions>): void;
135-
setFieldError: (field: keyof TValues, message: string | string[] | undefined) => void;
136-
setErrors: (fields: FormErrors<TValues>) => void;
137-
setValues<T extends keyof TValues>(fields: Partial<Record<T, TValues[T]>>): void;
138-
setFieldTouched: (field: keyof TValues, isTouched: boolean) => void;
139-
setTouched: (fields: Partial<Record<keyof TValues, boolean>>) => void;
140-
resetForm: (state?: Partial<FormState<TValues>>) => void;
141-
}
142-
143-
interface SubmissionContext<TValues extends Record<string, any>> extends FormActions<TValues> {
144-
evt?: Event;
145-
}
146-
147-
type SubmissionHandler<TValues extends Record<string, any>, TReturn = unknown> = (
148-
values: TValues,
149-
ctx: SubmissionContext<TValues>
150-
) => TReturn;
151-
152-
interface InvalidSubmissionContext<TValues extends Record<string, any>> {
153-
values: TValues;
154-
evt?: Event;
155-
errors: Partial<Record<keyof TValues, string>>;
156-
results: Partial<Record<keyof TValues, ValidationResult>>;
157-
}
158-
159-
interface FormValidationResult<TValues> {
160-
valid: boolean;
161-
results: Partial<Record<keyof TValues, ValidationResult>>;
162-
errors: Partial<Record<keyof TValues, string>>;
163-
}
164-
165-
interface FormMeta<TValues extends Record<string, any>> {
166-
touched: boolean;
167-
dirty: boolean;
168-
valid: boolean;
169-
validated: boolean;
170-
pending: boolean;
171-
initialValues?: TValues;
172-
}
173-
174-
type InvalidSubmissionHandler<TValues extends GenericObject = GenericObject> = (
175-
ctx: InvalidSubmissionContext<TValues>
176-
) => void;
177-
178-
type HandleSubmitFactory<TValues extends GenericObject> = <TReturn = unknown>(
179-
cb: SubmissionHandler<TValues, TReturn>,
180-
onSubmitValidationErrorCb?: InvalidSubmissionHandler<TValues>
181-
) => (e?: Event) => Promise<TReturn | undefined>;
182-
183-
type useForm = (opts?: FormOptions) => {
184-
values: TValues; // current form values
185-
submitCount: Ref<number>; // the number of submission attempts
186-
errors: ComputedRef<FormErrors<TValues>>; // first error message for each field
187-
errorBag: ComputedRef<Partial<Record<string, string[]>>>; // all error messages for each field
188-
meta: ComputedRef<FormMeta<TValues>>; // aggregate of the field's meta information
189-
isSubmitting: Ref<boolean>; // if the form submission function is being run
190-
isValidating: Ref<boolean>; // if the form validate function is being run
191-
setFieldValue<T extends keyof TValues>(field: T, value: TValues[T]): void; // Sets a field value
192-
setFieldError: (field: keyof TValues, message: string | string[] | undefined) => void; // Sets an error message for a field
193-
setErrors: (fields: FormErrors<TValues>) => void; // Sets error messages for fields
194-
setValues<T extends keyof TValues>(fields: Partial<Record<T, TValues[T]>>): void; // Sets multiple fields values
195-
setFieldTouched: (field: keyof TValues, isTouched: boolean) => void; // Sets a field's touched state
196-
setTouched: (fields: Partial<Record<keyof TValues, boolean>>) => void; // Sets multiple fields touched state
197-
resetForm: (state?: Partial<FormState<TValues>>) => void; // resets form state to the initial state or the given one
198-
handleReset: () => void; // Resets all fields' errors and meta and their values to their initial state
199-
submitForm: (e: Event) => void; // Forces submission of a form after successful validation (calls e.target.submit())
200-
validate(opts?: Partial<ValidationOptions>): Promise<FormValidationResult<TValues>>;
201-
validateField(field: keyof TValues): Promise<ValidationResult>;
202-
useFieldModel<TPath extends keyof TValues>(path: MaybeRef<TPath>): Ref<TValues[TPath]>;
203-
useFieldModel<TPath extends keyof TValues>(paths: [...MaybeRef<TPath>[]]): MapValues<typeof paths, TValues>;
204-
handleSubmit: HandleSubmitFactory<TValues> & { withControlled: HandleSubmitFactory<TValues> };
205-
};
206-
```
207-
20883
### Arguments
20984

21085
<CodeTitle level="4">
@@ -705,10 +580,16 @@ handleReset();
705580

706581
<CodeTitle level="4">
707582

708-
`useFieldModel` <DocBadge title="v4.6" />
583+
`useFieldModel`
709584

710585
</CodeTitle>
711586

587+
<DocTip>
588+
589+
This is deprecated, please use `defineInputBinds` or `defineComponentBinds` instead.
590+
591+
</DocTip>
592+
712593
This creates a bindable two-way model value for the specified fields, there are a couple of signatures for `useFieldModel`. Must be called in the `setup` function.
713594

714595
`useFieldModel` accepts either a single field path or multiple via an array. You can use either root field paths or nested paths like `some.user.path` with dot notation.
@@ -724,7 +605,6 @@ This creates a bindable two-way model value for the specified fields, there are
724605
725606
<script setup>
726607
import { useForm } from 'vee-validate';
727-
import MyTextInput from '@/components/MyTextInput.vue';
728608
729609
const { errors, useFieldModel } = useForm();
730610
@@ -735,3 +615,66 @@ const password = useFieldModel('password');
735615
const [email, password] = useFieldModel(['email', 'password']);
736616
</script>
737617
```
618+
619+
<CodeTitle level="4">
620+
621+
`defineInputBinds`
622+
623+
</CodeTitle>
624+
625+
This creates a bindable object for the specified field. The bindable object only works with native HTML input elements, for components use `defineComponentBinds` instead.
626+
627+
The `defineInputBinds` must be called in the `setup` function.
628+
629+
`defineInputBinds` accepts a single field path, You can use either root field paths or nested paths like `some.user.path` with dot notation.
630+
631+
```vue
632+
<template>
633+
<input v-bind="email" />
634+
<span>{{ errors.email }}</span>
635+
636+
<input v-bind="password" type="password" />
637+
<span>{{ errors.password }}</span>
638+
</template>
639+
640+
<script setup>
641+
import { useForm } from 'vee-validate';
642+
643+
const { errors, defineInputBinds } = useForm();
644+
645+
const email = defineInputBinds('email');
646+
const password = defineInputBinds('password');
647+
</script>
648+
```
649+
650+
<CodeTitle level="4">
651+
652+
`defineComponentBinds`
653+
654+
</CodeTitle>
655+
656+
This creates a bindable object for the specified field. The bindable object only works with components, for native HTML input elements use `defineInputBinds` instead.
657+
658+
The `defineComponentBinds` must be called in the `setup` function.
659+
660+
`defineComponentBinds` accepts a single field path, You can use either root field paths or nested paths like `some.user.path` with dot notation.
661+
662+
```vue
663+
<template>
664+
<MyTextInput v-bind="email" />
665+
<span>{{ errors.email }}</span>
666+
667+
<MyTextInput v-bind="password" type="password" />
668+
<span>{{ errors.password }}</span>
669+
</template>
670+
671+
<script setup>
672+
import { useForm } from 'vee-validate';
673+
import MyTextInput from '@/components/MyTextInput.vue';
674+
675+
const { errors, defineComponentBinds } = useForm();
676+
677+
const email = defineComponentBinds('email');
678+
const password = defineComponentBinds('password');
679+
</script>
680+
```

docs/src/pages/examples/using-stores.mdx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,4 @@ If you want to integrate vee-validate with state management solutions you can do
1919

2020
The example integrates a form state into the store by utilizing a setup function when defining a store. This makes vee-validate act as a state provider for the form where the form values become your store state and submit function becomes your store action.
2121

22-
<DocTip>
23-
24-
Notice the errors are displayed immediately, this is because when using `useFieldModel` vee-validate can no longer detect the field touched state. If you want to control the validation behavior in this case, then you need to implement a custom component with `useField` or `<Field>`.
25-
26-
</DocTip>
27-
2822
<LiveExample client:visible id="vee-validate-v4-pinia" />

docs/src/pages/guide/composition-api/handling-forms.mdx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,6 @@ const { handleSubmit, controlledValues } = useForm();
252252

253253
const onSubmit = handleSubmit(async () => {
254254
// Send only controlled values to the API
255-
// Only fields declared with `useField` or `useFieldModel` will be sent
256255
const response = await client.post('/users/', controlledValues.value);
257256
});
258257
```
@@ -264,7 +263,6 @@ const { handleSubmit } = useForm();
264263

265264
const onSubmit = handleSubmit.withControlled(async values => {
266265
// Send only controlled values to the API
267-
// Only fields declared with `useField` or `useFieldModel` will be sent
268266
const response = await client.post('/users/', values);
269267
});
270268
```

0 commit comments

Comments
 (0)