Skip to content

feat(form-core): Validation on first and consequent attempts #1286

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

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions packages/form-core/src/FormApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,15 @@ export interface FormOptions<
TOnSubmit,
TOnSubmitAsync
>
/**
* Specifies which validation type to use on the first submission attempt
*/
validationOnFirstAttempt?: ValidationCause;

/**
* Specifies which validation type to use on subsequent submission attempts
*/
validationOnConsequentAttempts?: ValidationCause;
/**
* A function to be called when the form is submitted, what should happen once the user submits a valid form returns `any` or a promise `Promise<any>`
*/
Expand Down Expand Up @@ -1500,7 +1509,12 @@ export class FormApi<
this.baseStore.setState((prev) => ({ ...prev, isSubmitting: false }))
}

await this.validateAllFields('submit')
//
const validationType = this.state.submissionAttempts === 1
? this.options.validationOnFirstAttempt ?? 'submit'
: this.options.validationOnConsequentAttempts ?? 'submit';
Comment on lines +1514 to +1515
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for me something like validationType and revalidationType sound more intuitive

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright I'll seek some approval about this and let you know!


await this.validateAllFields(validationType)

if (!this.state.isFieldsValid) {
done()
Expand All @@ -1511,7 +1525,7 @@ export class FormApi<
return
}

await this.validate('submit')
await this.validate(validationType)

// Fields are invalid, do not submit
if (!this.state.isValid) {
Expand Down
Loading