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

Dicanio's avatar

Displaying repeating error/message in fading toast?

Hello, i just started creating my first app using Inertia with Vue and i encountered problem that i cant solve. I want to display errors in component that will fade away after few secs. If i use <Toast v-for="error in this.$page.props.errors /> and then in Toast component in mounted use setTimeout to set visibility of error it correctly appears but i cant display a toast with same error again until I change the route. For example lets say i want to register with email thats been already taken, if i click submit with taken email i get that message, but if i click it again i wont receive it. How can i solve it?

0 likes
1 reply
LaryAI's avatar
Level 58

The issue is that the errors array is not being updated when the same error occurs again. One solution is to add a unique identifier to each error message and use that to check if the error has already been displayed. Here's an example implementation:

  1. In your controller, add a unique identifier to each error message:
return redirect()->back()->withErrors(['email' => 'Email already taken.'])->with('errorId', uniqid());
  1. In your Vue component, add a data property to keep track of displayed errors:
data() {
  return {
    displayedErrors: []
  }
}
  1. Update your template to check if the error has already been displayed:
<template>
  <div>
    <toast v-for="error in this.$page.props.errors" :key="error.id" v-if="!displayedErrors.includes(error.id)">
      {{ error }}
    </toast>
  </div>
</template>
  1. In your mounted hook, add the error id to the displayedErrors array when the toast is displayed:
mounted() {
  this.$nextTick(() => {
    this.$refs.toast.forEach(toast => {
      const errorId = toast.getAttribute('data-error-id')
      this.displayedErrors.push(errorId)
      setTimeout(() => {
        this.displayedErrors = this.displayedErrors.filter(id => id !== errorId)
      }, 5000)
    })
  })
}

This implementation adds a unique id to each error message and checks if it has already been displayed before showing the toast. The displayedErrors array is updated when a toast is displayed and removed when it is hidden.

Please or to participate in this conversation.