Skip to content

Commit a18c19f

Browse files
authored
feat: allow path meta querying for nested fields closes #4575 (#4594)
1 parent abf0584 commit a18c19f

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

.changeset/shy-scissors-hug.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"vee-validate": patch
3+
---
4+
5+
feat: allow path meta querying for nested fields closes #4575

packages/vee-validate/src/useForm.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -719,15 +719,31 @@ export function useForm<
719719
}
720720

721721
function isFieldTouched(field: Path<TValues>) {
722-
return !!findPathState(field)?.touched;
722+
const pathState = findPathState(field);
723+
if (pathState) {
724+
return pathState.touched;
725+
}
726+
727+
// Find all nested paths and consider their touched state
728+
return pathStates.value.filter(s => s.path.startsWith(field)).some(s => s.touched);
723729
}
724730

725731
function isFieldDirty(field: Path<TValues>) {
726-
return !!findPathState(field)?.dirty;
732+
const pathState = findPathState(field);
733+
if (pathState) {
734+
return pathState.dirty;
735+
}
736+
737+
return pathStates.value.filter(s => s.path.startsWith(field)).some(s => s.dirty);
727738
}
728739

729740
function isFieldValid(field: Path<TValues>) {
730-
return !!findPathState(field)?.valid;
741+
const pathState = findPathState(field);
742+
if (pathState) {
743+
return pathState.valid;
744+
}
745+
746+
return pathStates.value.filter(s => s.path.startsWith(field)).every(s => s.valid);
731747
}
732748

733749
/**

packages/vee-validate/tests/useForm.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,8 @@ describe('useForm()', () => {
11831183
setup() {
11841184
form = useForm();
11851185
useField('fname');
1186+
useField('nested.lname');
1187+
useField('nested.fname');
11861188

11871189
return {};
11881190
},
@@ -1192,10 +1194,13 @@ describe('useForm()', () => {
11921194
await flushPromises();
11931195
expect(form.meta.value.touched).toBe(false);
11941196
expect(form.isFieldTouched('fname')).toBe(false);
1197+
expect(form.isFieldTouched('nested')).toBe(false);
11951198
form.setFieldTouched('fname', true);
1199+
form.setFieldTouched('nested.lname', true);
11961200
await flushPromises();
11971201
expect(form.meta.value.touched).toBe(true);
11981202
expect(form.isFieldTouched('fname')).toBe(true);
1203+
expect(form.isFieldTouched('nested')).toBe(true);
11991204
});
12001205

12011206
test('can query field dirty state', async () => {
@@ -1204,6 +1209,8 @@ describe('useForm()', () => {
12041209
setup() {
12051210
form = useForm();
12061211
useField('fname');
1212+
useField('nested.lname');
1213+
useField('nested.fname');
12071214

12081215
return {};
12091216
},
@@ -1213,10 +1220,13 @@ describe('useForm()', () => {
12131220
await flushPromises();
12141221
expect(form.meta.value.dirty).toBe(false);
12151222
expect(form.isFieldDirty('fname')).toBe(false);
1223+
expect(form.isFieldDirty('nested')).toBe(false);
12161224
form.setFieldValue('fname', 'value');
1225+
form.setFieldValue('nested.lname', 'value');
12171226
await flushPromises();
12181227
expect(form.meta.value.dirty).toBe(true);
12191228
expect(form.isFieldDirty('fname')).toBe(true);
1229+
expect(form.isFieldDirty('nested')).toBe(true);
12201230
});
12211231

12221232
test('can query field valid state', async () => {
@@ -1225,6 +1235,8 @@ describe('useForm()', () => {
12251235
setup() {
12261236
form = useForm();
12271237
useField('fname');
1238+
useField('nested.lname');
1239+
useField('nested.fname');
12281240

12291241
return {};
12301242
},
@@ -1234,10 +1246,13 @@ describe('useForm()', () => {
12341246
await flushPromises();
12351247
expect(form.meta.value.valid).toBe(true);
12361248
expect(form.isFieldValid('fname')).toBe(true);
1249+
expect(form.isFieldValid('nested')).toBe(true);
12371250
form.setFieldError('fname', 'ERROR');
1251+
form.setFieldError('nested.lname', 'ERROR');
12381252
await flushPromises();
12391253
expect(form.meta.value.valid).toBe(false);
12401254
expect(form.isFieldValid('fname')).toBe(false);
1255+
expect(form.isFieldValid('nested')).toBe(false);
12411256
});
12421257

12431258
// #4438

0 commit comments

Comments
 (0)