Skip to content
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

[FEAT] give errors from submit() exception #73

Merged
merged 8 commits into from
Oct 29, 2024
Merged

Conversation

sandhya-spend
Copy link
Contributor

GS-8643

The exception thrown by form.submit() can also be used to populate errors on the fields.

Usage:

import {ValidationError} from 'mobx-autoform'
submit = (snapshot, form) => {
// `errors` is an object where the keys are the fields and the values are arrays of errors for the field.
const errors = {}
throw new ValidationError("Submission failed", errors)
}

@sandhya-spend sandhya-spend self-assigned this Oct 18, 2024
src/index.js Outdated Show resolved Hide resolved
src/index.js Outdated
Comment on lines 193 to 195
return Promise.resolve(configSubmit(form.getSnapshot(), form)).catch(
err => handleSubmitErr(state, err)
)
Copy link
Member

@stellarhoof stellarhoof Oct 18, 2024

Choose a reason for hiding this comment

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

Looks like I was wrong. Command does support async functions, so you don't have to deal with Promise.resolve and catch.

Also, you can use

        if (err instanceof ValidationError) {
          state.errors = {
            '': [err.message],
            ...err.cause,
          }
        }

to use the error message as the message for the entire form.

Screenshot 2024-10-18 at 3 45 00 PM

@decrapifier
Copy link
Contributor

Warnings
⚠️ You should add at least 1 more reviewer to the PR

Coverage Report

👍 Test coverage is looking good.

Files % Stmts % Branch % Funcs % Lines Uncovered Lines
src/futil.js 100 100 100 100
src/index.js 100 96.61 96.67 100
src/mobx.js 94.87 88.24 100 94.87 10, 11
and 2 more...
Files % Stmts % Branch % Funcs % Lines Uncovered Lines
src/util.js 100 100 100 100
src/validators.js 52.38 100 50 52.38 6, 7, 8, 9, 10, 11, 12, 13, 14, 15

Generated by 🚫 dangerJS against 65d4311

@smartprocure smartprocure deleted a comment from decrapifier Oct 29, 2024
@smartprocure smartprocure deleted a comment from decrapifier Oct 29, 2024
@smartprocure smartprocure deleted a comment from decrapifier Oct 29, 2024
@smartprocure smartprocure deleted a comment from decrapifier Oct 29, 2024
@smartprocure smartprocure deleted a comment from decrapifier Oct 29, 2024
Copy link
Member

@stellarhoof stellarhoof left a comment

Choose a reason for hiding this comment

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

Thanks @sandhya-spend. This is a nice improvement to our form library.

@stellarhoof stellarhoof merged commit 7ea4571 into master Oct 29, 2024
2 checks passed
@stellarhoof stellarhoof deleted the GS-8643/submit_error branch October 29, 2024 13:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants