Skip to content

Commit

Permalink
fix: errors should no longer throw on intial render (#508)
Browse files Browse the repository at this point in the history
  • Loading branch information
crutchcorn authored Nov 5, 2023
1 parent 0982e3f commit 0dc440d
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions packages/form-core/src/FormApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,21 @@ export class FormApi<TFormData, ValidatorType> {

mount = () => {
const doValidate = () => {
if (typeof this.options.onMount === 'function') {
return this.options.onMount(this.state.values, this)
}
if (this.options.validator) {
if (
this.options.validator &&
typeof this.options.onMount !== 'function'
) {
return (this.options.validator as Validator<TFormData>)().validate(
this.state.values,
this.options.onMount,
)
}
return (this.options.onMount as ValidateFn<TFormData, ValidatorType>)(
this.state.values,
this,
)
}
if (!this.options.onMount) return
const error = doValidate()
if (error) {
this.store.setState((prev) => ({
Expand Down Expand Up @@ -269,17 +274,16 @@ export class FormApi<TFormData, ValidatorType> {

const errorMapKey = getErrorMapKey(cause)
const doValidate = () => {
if (typeof validate === 'function') {
return validate(this.state.values, this) as ValidationError
}
if (this.options.validator && typeof validate !== 'function') {
return (this.options.validator as Validator<TFormData>)().validate(
this.state.values,
validate,
)
}
throw new Error(
`Form validation for ${errorMapKey} failed. ${errorMapKey} should either be a function, or \`validator\` should be correct.`,

return (validate as ValidateFn<TFormData, ValidatorType>)(
this.state.values,
this,
)
}

Expand Down

0 comments on commit 0dc440d

Please sign in to comment.