Skip to content

Commit 3c76bb2

Browse files
committed
fix: avoid inserting value binding for file type inputs closes #3760
1 parent 2751552 commit 3c76bb2

3 files changed

Lines changed: 45 additions & 4 deletions

File tree

packages/vee-validate/src/Field.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,11 @@ const FieldImpl = defineComponent({
189189

190190
if (hasCheckedAttr(ctx.attrs.type) && checked) {
191191
attrs.checked = checked.value;
192-
} else {
193-
attrs.value = value.value;
194192
}
195193

196194
const tag = resolveTag(props, ctx);
197195
if (shouldHaveValueBinding(tag, ctx.attrs)) {
198-
delete attrs.value;
196+
attrs.value = value.value;
199197
}
200198

201199
return attrs;

packages/vee-validate/src/utils/assertions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export function isNativeMultiSelectNode(tag: string, attrs: Record<string, unkno
8383
* For multi-selects because the value binding will reset the value
8484
*/
8585
export function shouldHaveValueBinding(tag: string, attrs: Record<string, unknown>) {
86-
return isNativeMultiSelectNode(tag, attrs) || isFileInputNode(tag, attrs);
86+
return !isNativeMultiSelectNode(tag, attrs) && attrs.type !== 'file' && !hasCheckedAttr(attrs.type);
8787
}
8888

8989
export function isFormSubmitEvent(evt: unknown): evt is Event & { target: HTMLFormElement } {

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,4 +1165,47 @@ describe('<Field />', () => {
11651165
expect(error.textContent).toBe('');
11661166
expect(input.value).toBe('');
11671167
});
1168+
1169+
test('should have correct field object binding properties based on file type', async () => {
1170+
const textualType = jest.fn();
1171+
const fileType = jest.fn();
1172+
const checkboxType = jest.fn();
1173+
const radioType = jest.fn();
1174+
1175+
mountWithHoc({
1176+
template: `
1177+
<div>
1178+
<Field name="text" v-slot="{ field }">
1179+
{{ textualType(field) }}
1180+
</Field>
1181+
<Field name="file" type="file" v-slot="{ field }">
1182+
{{ fileType(field) }}
1183+
</Field>
1184+
<Field name="checkbox" type="checkbox" v-slot="{ field }">
1185+
{{ checkboxType(field) }}
1186+
</Field>
1187+
<Field name="radio" type="radio" v-slot="{ field }">
1188+
{{ radioType(field) }}
1189+
</Field>
1190+
</div>
1191+
`,
1192+
setup() {
1193+
return {
1194+
textualType,
1195+
fileType,
1196+
checkboxType,
1197+
radioType,
1198+
};
1199+
},
1200+
});
1201+
1202+
await flushPromises();
1203+
const lastCallOf = (fn: ReturnType<typeof jest.fn>) => fn.mock.calls[fn.mock.calls.length - 1][0];
1204+
expect(lastCallOf(textualType)).toHaveProperty('value');
1205+
expect(lastCallOf(fileType)).not.toHaveProperty('value');
1206+
expect(lastCallOf(checkboxType)).toHaveProperty('checked');
1207+
expect(lastCallOf(checkboxType)).not.toHaveProperty('value');
1208+
expect(lastCallOf(radioType)).toHaveProperty('checked');
1209+
expect(lastCallOf(radioType)).not.toHaveProperty('value');
1210+
});
11681211
});

0 commit comments

Comments
 (0)