Skip to content

How to use readSelection? #278

Answered by mskocik
galaczi asked this question in Q&A
Jan 24, 2025 · 2 comments · 6 replies
Discussion options

You must be logged in to vote

Starting a new thread which fixes all your problems.

Fix schema in +page.server.js

const schema = z.object({
  id: z.number().nullable().transform((value, ctx) => {
      if (value === null) {
        ctx.addIssue({
          code: z.ZodIssueCode.custom,
          fatal: true,
          message: 'ID is required'
        });
      }
      return value;
  }),
  missing: z.string().nonempty('Missing is required') // so validation fails
});

Now when validation fails on server, item is kept in Svelecte properly.

⚠️ If you plan to use superForm with resetForm: false you need this additional code to bypass superform's setting value to null and to correct value again:

<script> section:

let cached…

Replies: 2 comments 6 replies

Comment options

You must be logged in to vote
4 replies
@galaczi
Comment options

galaczi Jan 27, 2025
Author Sponsor

@mskocik
Comment options

@galaczi
Comment options

galaczi Jan 27, 2025
Author Sponsor

@mskocik
Comment options

Comment options

You must be logged in to vote
2 replies
@mskocik
Comment options

@galaczi
Comment options

galaczi Jan 28, 2025
Author Sponsor

Answer selected by galaczi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants