-
-
Notifications
You must be signed in to change notification settings - Fork 66
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
$submitting isn't reset when there is an error. #485
Comments
I can't reproduce this, using this test:
When I do the test, submitting is reset to false for all events. |
I'll try to create a sample. Also while looking at this, noticed you cannot use onError to update the message as form is missing.
|
Yes, there is no data for the form to update as an exception was thrown, you have to set |
Here is a sample https://www.sveltelab.dev/wio08v2eu7gokiy that shows off this issue and 4 other issues when using SPA mode
|
Here's an updated version of the sample that explains most of the problems you're having in the comments: https://www.sveltelab.dev/g1vrs75tk8e7lkb Relevant code part, for reference: const { form, errors, message, enhance, submit, submitting } = superForm(
defaults({ email: '[email protected]' }, zod(schema)),
{
onError(event) {
console.log('=== onError ===');
/*
1. It's not possible to use the SuperValidated data from onError,
as it can be caught in onSubmit where it doesn't exist.
You need to update the stores directly.
*/
$message = JSON.stringify(event, null, 2);
// Cast the error, as its type isn't unknown.
const error = event.result.error as Record<string, unknown>;
$errors = error.errors as Record<string, string[]>;
},
async onUpdate({ form, result }) {
console.log('=== onUpdate ===');
if (!form.valid) {
console.log('Invalid form, returning early.');
return;
}
const isSuccess = Math.random() >= 0.5;
console.log('isSuccess', isSuccess);
if (isSuccess) {
// 2. Updating form.data does work, the data wasn't modified in your example:
// 5. This only works if resetForm: false. Otherwise the form will reset.
form.data = {
name: form.data.name + ' Updated',
email: form.data.email.replace('.com', '.org')
};
} else {
const madeupProblemDetails = {
type: 'https://example.com/probs/validation-error',
title: 'Validation Error',
status: 422,
detail: 'There are validation errors in your request.',
instance: '/your/instance/uri',
errors: {
name: ['String must contain at least 2 character(s)'],
email: ['Invalid email']
}
};
const shouldThrow = Math.random() >= 0.5;
console.log('shouldThrow', shouldThrow);
if (shouldThrow) {
// 3. This will update the status in the next release:
result.status = 422;
throw madeupProblemDetails;
}
setError(form, 'name', ['random error']);
result.status = 422;
result.type = 'failure';
// 3. Cannot set data to anything else than a SuperValidated object. In general, tamper as little as possible with the result
// 4. Focus will be kept when this is commented out.
//result.data = madeupProblemDetails;
}
},
SPA: true,
validators: zod(schema),
resetForm: false
}
);
const debouncedSubmit = debounce(500, submit); Most of these problems aren't bugs, so if you're using this in a commercial project, please consider making a donation. |
Description
I made a request which threw an exception during onUpdate.
submitting
was not reset and so I always have a loading spinner.On side note, I tried to look where submitting was set (didn't see any references)
The text was updated successfully, but these errors were encountered: