Skip to content

Commit

Permalink
🐞 fix react-hook-form#4019 validate function pass processed value (re…
Browse files Browse the repository at this point in the history
  • Loading branch information
bluebill1049 authored Jan 25, 2021
1 parent ef0f6b2 commit 0562212
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/logic/getFieldValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default function getFieldValue<TFieldValues extends FieldValues>(
name: InternalFieldName<TFieldValues>,
shallowFieldsStateRef?: React.MutableRefObject<Partial<FieldValues>>,
excludeDisabled?: boolean,
shouldKeepRawValue?: boolean,
) {
const field = fieldsRef.current[name]!;

Expand Down Expand Up @@ -46,7 +47,9 @@ export default function getFieldValue<TFieldValues extends FieldValues>(
return getCheckboxValue(field.options).value;
}

return valueAsNumber
return shouldKeepRawValue
? value
: valueAsNumber
? value === ''
? NaN
: +value
Expand Down
8 changes: 7 additions & 1 deletion src/logic/validateField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,13 @@ export default async <TFieldValues extends FieldValues>(
}

if (validate) {
const fieldValue = getFieldsValue(fieldsRef, name, shallowFieldsStateRef);
const fieldValue = getFieldsValue(
fieldsRef,
name,
shallowFieldsStateRef,
false,
true,
);
const validateRef = isRadioOrCheckbox && options ? options[0].ref : ref;

if (isFunction(validate)) {
Expand Down

0 comments on commit 0562212

Please sign in to comment.