Giter VIP home page Giter VIP logo

Comments (1)

kasokz avatar kasokz commented on June 20, 2024 1

I also had the same problem, when trying to use the enhanced forms feature from svelte-kit.
I couldn't get it to work through default configurations but I found a workaround that I use as a pattern, when I use svelte-forms, maybe you can incorporate this in your project:

My form definition is only slightly longer in a function like so:

<script lang="ts">
export let data;
...
function createEditProfileForm() {
		const firstName = field('firstName', data.userProfile?.first_name, [required()]);
		const lastName = field('lastName', data.userProfile?.last_name, [required()]);
		const zipCode = field('zipCode', data.userProfile?.zip_code, [required()]);
		const city = field('city', data.userProfile?.city, [required()]);
		const address = field('address', data.userProfile?.address, [required()]);
		const editProfileForm = svelteForm(zipCode, city, address, firstName, lastName);
		return { firstName, lastName, zipCode, city, address, editProfileForm };
}
function resetEditProfileForm() {
                ({ firstName, lastName, zipCode, city, address, editProfileForm } = createEditProfileForm());
}
let { firstName, lastName, zipCode, city, address, editProfileForm } = createEditProfileForm();
...
</script>

And the markup is like this:

...
<form action="/?someAction" use:enhance={() => {
   return async ({ update }) => {
     // this causes the server load function to run again without resetting the 
     // form. The default reset unfortunately completely clears my forms, so I
     // needed to manually reset it, but with fresh values from the server.
     await update({ reset: false, invalidateAll: true });  
    // after the update Promise resolves the PageData is refreshed, 
    // so I fully recreate the form. This has the additional advantage that the form is not dirty again, 
    // so something like disabling buttons when the user didn't do anything yet just works.
     resetEditProfileForm();  
   };
}>
   <input bind:value={$firstName.value} />
   ...
   <button type="submit">Update Profile</button>
....
</form>
...

Either I missed something in the official docs or it's really missing, but I initially tried to find some Event, that tells me that the load functions are done executing after invalidation. Unfortunately I couldn't find something so I had to customize my enhance action.
But overall I'm very satisfied with my workaround since it only minimally affects my personal DX.

from svelte-forms.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.