Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

671
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda