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

$submitting isn't reset when there is an error. #485

Closed
1 task done
niemyjski opened this issue Sep 21, 2024 · 5 comments
Closed
1 task done

$submitting isn't reset when there is an error. #485

niemyjski opened this issue Sep 21, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@niemyjski
Copy link
Contributor

  • Before posting an issue, read the FAQ and search the previous issues.

Description
I made a request which threw an exception during onUpdate. submitting was not reset and so I always have a loading spinner.

image

On side note, I tried to look where submitting was set (didn't see any references)

@niemyjski niemyjski added the bug Something isn't working label Sep 21, 2024
@ciscoheat
Copy link
Owner

I can't reproduce this, using this test:

import SuperDebug from '$lib/index.js';

When I do the test, submitting is reset to false for all events.

@niemyjski
Copy link
Contributor Author

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.

 onError({ form }) {
          setMessage(form, 'An error occurred while logging in, please try again.');
      },
        ```

@ciscoheat
Copy link
Owner

ciscoheat commented Sep 23, 2024

Yes, there is no data for the form to update as an exception was thrown, you have to set $message or other stores directly.

@niemyjski
Copy link
Contributor Author

niemyjski commented Oct 1, 2024

Here is a sample https://www.sveltelab.dev/wio08v2eu7gokiy that shows off this issue and 4 other issues when using SPA mode

  1. can't set form errors from onError or other functions defined (on change etc..),
  2. updating form.data doesn't work
  3. Have to set result.status instead of just throwing an error or setting a form error.
  4. focus lost on input text box
  5. submitting not reset.

@ciscoheat
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants