sveltekit - Call load function after form submit - Stack Overflow

I have a form that on submit does not trigger an action on sveltekit server but instead I have a handle

I have a form that on submit does not trigger an action on sveltekit server but instead I have a handleSubmit function on the svelte module for that.

<script>

    export let data;

    let parentObject = writable({
        name: '',
        childObjects: []
    });


    function addChildObject() {
        parentObject.update(s => {
            s.childObjects.push({ name: '', something: '' });
            return s;
        });
    }

    async function handleSubmit(event) {
        event.preventDefault();
        parentObject.subscribe(data => {
             ....
</script>

<form class="parent-object-form" method="POST" on:submit={handleSubmit}> 
    ...

    <button type="button" on:click={addChildObject}>
        Add Child Object
    </button>

    <button type="submit">Submit</button>
</form>

I specifically don't have a server action because I dynamically modify the form by adding and removing fields from the object.

Whenever I submit this, I want the load function in the +page.server.js server file to be called again, because I also need to render a list of parentObjects that I fetch from a server.

<table>
    <tbody>
        {#each data.parentObejct as parent}
             ...
        {/each}

I need this list to get updated after the form is submitted. How do I do that?

Is there a way to manually trigger the load function after the submit? Should I manually add the new object to the list on the handleSubmit func? Is there a way to manually pass this dynamic data to a server action?

I have a form that on submit does not trigger an action on sveltekit server but instead I have a handleSubmit function on the svelte module for that.

<script>

    export let data;

    let parentObject = writable({
        name: '',
        childObjects: []
    });


    function addChildObject() {
        parentObject.update(s => {
            s.childObjects.push({ name: '', something: '' });
            return s;
        });
    }

    async function handleSubmit(event) {
        event.preventDefault();
        parentObject.subscribe(data => {
             ....
</script>

<form class="parent-object-form" method="POST" on:submit={handleSubmit}> 
    ...

    <button type="button" on:click={addChildObject}>
        Add Child Object
    </button>

    <button type="submit">Submit</button>
</form>

I specifically don't have a server action because I dynamically modify the form by adding and removing fields from the object.

Whenever I submit this, I want the load function in the +page.server.js server file to be called again, because I also need to render a list of parentObjects that I fetch from a server.

<table>
    <tbody>
        {#each data.parentObejct as parent}
             ...
        {/each}

I need this list to get updated after the form is submitted. How do I do that?

Is there a way to manually trigger the load function after the submit? Should I manually add the new object to the list on the handleSubmit func? Is there a way to manually pass this dynamic data to a server action?

Share Improve this question edited Nov 20, 2024 at 16:16 jonrsharpe 122k30 gold badges267 silver badges474 bronze badges asked Nov 20, 2024 at 16:14 fedestfedest 1,3503 gold badges16 silver badges37 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

A form can be as dynamic as you want, that does not mean you cannot use form actions. You also can add things to the submitted form data via the enhance action.

If you use enhance, that already will trigger the load function, if you do not use it, you can manually call invalidateAll instead.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742345655a4426488.html

相关推荐

  • sveltekit - Call load function after form submit - Stack Overflow

    I have a form that on submit does not trigger an action on sveltekit server but instead I have a handle

    7小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信