Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

669
Visualizações
After redirect back to a form page, the old form values are still displayed on Inertia.js on Laravel 8

I am trying to make the system like this:

  1. Displays a first form page. After a user submit the form, redirects to the second form page.
  2. Displays the second form page. When a user click a delete button on the page, redirects to the first form page.

However, when I click the delete button in 2, the old form values are still displaying on the first form page. I tried following methods:

  1. preserveState:false on submit.
Inertia.post(props.deleteDraftUrl, null, {
                    preserveState: false
                });

this works for when I click a delete button on the first page, but when I click the one in the second page, the old form values are still displayed.

  1. use preserveState:false of the form made by useForm.
form.post(props.deleteDraftUrl, {
                    preserveState: false
                });

This doesn't work too.

  1. onSuccess: () => form.reset();
Inertia.post(props.deleteDraftUrl, null, {
                    preserveState: false,
                    onSuccess: () => form.reset(),
                });

This doesn't work too.

I have been stacked on this problem for a day. Any suggestions are helpful.

Updates: Added the entire code in the script of the component. The first page and the second page are pretty much the same. I also simplified and modified the code mostly for the security reasons.

import {useForm} from "@inertiajs/inertia-vue3";
import {Inertia} from "@inertiajs/inertia";

export default {
    props: {
        pageTitle: {
            type: String,
            required: true,
        },
        defaultValues: {
            type: Object,
            required: true,
        },
    },
    remember: ["form"],
    setup(props) {
        const form = useForm({
            value1: props.defaultValues.value1,
            value2: props.defaultValues.value2,
            value3: props.defaultValues.value3,
        });

        
        const deleteDraft = () => {
            if (confirm("delete the draft?")) {
                Inertia.post(props.deleteDraftUrl, null, {
                    preserveState: false,
                    onSuccess: () => console.log('success!'),
                });
            }
        };
        

        return {
            form,
            pageTitle,
            deleteDraft,
        };
    },
};
</script>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I found the problem and the answer. For some reasons, just another guy wrote a code that keeps the inputted values in the defaultValues variable at the first page's CONTROLLER! That's why it never clears form.

So the answer is : Making the process which keeps the inputted values in the defaultValues variable not working after submitting a deleting-draft form.

Sorry for the stupid cause, and "preserveState : false" seems to work fine.

about 4 years ago · Juan Pablo Isaza Relatório

0

Problem

You are passing the default values of your fields when initializing your form:

const form = useForm({
    value1: props.defaultValues.value1,
    value2: props.defaultValues.value2,
    value3: props.defaultValues.value3,
});

So when you reset the form, it will reset to those values.

Solution

You can update the defaults of your form before resetting.

Inertia.post(props.deleteDraftUrl, {
    onSuccess: () => {
        form.defaults({
            value1: null,
            value2: null,
            value3: null,
        });

        form.reset();
    },
});

If you have a lot of fields to reset, you can loop through the defaultValues to clear them.

Object.keys(form.data()).forEach(field => form.defaults(field, null));

form.reset();
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda