Be part of JetBrains PHPverse 2026 on June 9 โ€“ a free online event bringing PHP devs worldwide together.

Ethan.'s avatar

How to reset password field after validation fails with inertia

Hi guys, I'm learning the inertia.js series. I want to clear password field after validation fails. I've tried onError, onFinish and onSuccess. But these're not working as I expected. Please would you help me....๐Ÿ˜ข๐Ÿ˜ข

Login.vue


<script>
import { useForm } from "@inertiajs/inertia-vue3";

export default {
  layout: null,
  setup() {
    const form = useForm({
      email: "",
      password: "",
    });

    const submit = () => {
      form.post("login", form, {
		// not working ๐Ÿ˜ข
        onError: () => form.reset("password"),
	   // not working ๐Ÿ˜ข
        onFinish: () => form.reset("password"),
       // not working ๐Ÿ˜ข
        onSuccess: () => form.reset("password"),
      });
    };

    return { form, submit };
  },
};
</script>
0 likes
7 replies
anilkumarthakur60's avatar
   <form @submit.prevent="login">
  <input     type="email" v-model="form.email"/>          
              <div class="invalid-feedback d-flex" v-if="form.errors.email">
                {{ form.errors.email }}
              </div>


 <input type="password" v-model="form.password" />
  <div class="invalid-feedback d-flex" v-if="form.errors.password">
                {{ form.errors.password }}
              </div>
<button type="submit" >Login</button>

</form>

    const form = useForm({
      email: null,
      password: null,
    });
    return {  form };
  },




  methods: {
    login() {
      this.form
        .transform((data) => ({
          ...data,
          remember: this.form.remember ? "on" : "",
        }))
        .post(this.route("login"), {
          onFinish: () => this.form.reset("password"),

          onSuccess: () => {
            this.$swal({
              icon: "success",
              title: "Login Successfully",
              showConfirmButton: false,
              timer: 1500,
            });
          },
        });
    },
  },
1 like
anilkumarthakur60's avatar

its modified Login System .. app\Providers\FortifyServiceProvider.php

   Fortify::loginView(function () {
            return Inertia::render('Authentication/Login);
        });

it should work

1 like
Ethan.'s avatar

@anilkumarthakur60 Okay sir, Fortify package was not used in my project. I wrote Login System manually. When I will use Fortify, I will try that. Thank you so much sir ๐Ÿงก

isimmons's avatar

@Ethan. I'm doing this course now. I did

watch(form, () => {
    if(form.hasErrors)
        form.reset("password");
})

Thanks for the example

1 like
Ethan.'s avatar

@isimmons yeah bro great ๐Ÿ˜, I miswrote the code.

	// before
    const submit = () => {
      form.post("login", form, {
		// not working ๐Ÿ˜ข
        onError: () => form.reset("password"),
	   // not working ๐Ÿ˜ข
        onFinish: () => form.reset("password"),
       // not working ๐Ÿ˜ข
        onSuccess: () => form.reset("password"),
      });
    };

	// after
   const submit = () => {
      form.post("login", {
		// will work ๐Ÿ˜ข
        onError: () => form.reset("password"),
	   // will work ๐Ÿ˜ข
        onFinish: () => form.reset("password"),
       // will work ๐Ÿ˜ข
        onSuccess: () => form.reset("password"),
      });
    };

we don't need form object as a second parameter and also we don't need to watch either anymore I didn't read the doc carefully ๐Ÿ˜๐Ÿ˜

1 like

Please or to participate in this conversation.