-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix: show validation error when field array is empty (#4981) #5143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "vee-validate": patch | ||
| --- | ||
|
|
||
| Fix error message not showing when field array is empty (#4981) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -521,6 +521,72 @@ test('array move initializes the array if undefined', async () => { | |
| expect(arr.fields.value).toHaveLength(0); | ||
| }); | ||
|
|
||
| // #4981 | ||
| test('shows validation error when field array is empty after removing all items', async () => { | ||
| let form!: FormContext; | ||
| let arr!: FieldArrayContext; | ||
| mountWithHoc({ | ||
| setup() { | ||
| form = useForm<any>({ | ||
| initialValues: { | ||
| users: ['one'], | ||
| }, | ||
| validationSchema: z.object({ | ||
| users: z.array(z.string()).min(1, 'At least one item is required'), | ||
| }), | ||
| }); | ||
|
|
||
| arr = useFieldArray('users'); | ||
| }, | ||
| template: ` | ||
| <div></div> | ||
| `, | ||
| }); | ||
|
Comment on lines
+528
to
+544
|
||
|
|
||
| await flushPromises(); | ||
| expect(form.meta.value.valid).toBe(true); | ||
| arr.remove(0); | ||
| await flushPromises(); | ||
| expect(form.meta.value.valid).toBe(false); | ||
| expect(form.errors.value['users']).toBe('At least one item is required'); | ||
| }); | ||
|
|
||
| // #4981 | ||
| test('shows validation error at correct path when nested field array is empty', async () => { | ||
| let form!: FormContext; | ||
| let arr!: FieldArrayContext; | ||
| mountWithHoc({ | ||
| setup() { | ||
| form = useForm<any>({ | ||
| initialValues: { | ||
| settings: { | ||
| items: ['one'], | ||
| }, | ||
| }, | ||
| validationSchema: z.object({ | ||
| settings: z.object({ | ||
| items: z.array(z.string()).min(1, 'At least one item is required'), | ||
| }), | ||
| }), | ||
| }); | ||
|
|
||
| arr = useFieldArray('settings.items'); | ||
| }, | ||
| template: ` | ||
| <div></div> | ||
| `, | ||
| }); | ||
|
|
||
| await flushPromises(); | ||
| expect(form.meta.value.valid).toBe(true); | ||
| arr.remove(0); | ||
| await flushPromises(); | ||
| expect(form.meta.value.valid).toBe(false); | ||
| // The error should be at 'settings.items', NOT at 'settings' | ||
| expect(form.errors.value['settings.items']).toBe('At least one item is required'); | ||
| expect(form.errors.value['settings']).toBeUndefined(); | ||
| }); | ||
|
|
||
| // #4557 | ||
| test('errors are available to the newly inserted items', async () => { | ||
| let arr!: FieldArrayContext; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isFieldArrayPathcomparestoValue(a.path)andexpectedPathas raw strings. Standard-schema issues are converted usinggetDotPath, which yields dot-index paths (e.g.users.0.friends), while vee-validate commonly normalizes array indices to bracket syntax (e.g.users[0].friends). If a field array path includes numeric indices, this equality check will fail and hoisting will still occur, leaving the original bug unfixed for those cases. Consider normalizing both sides (and ideally precomputing a normalized Set of field array paths once per validation run) before doing the comparison.