When a file input validation fails in a Laravel project, the user is redirected back to the current page. However, the issue arises when the user has to scroll down to see the error message and understand what went wrong.
This doesn't provide the best user experience, so I came up with an idea to automatically scroll down to where the mistake was made using JavaScript upon redirection to the page.
I can achieve this by utilizing the scrollIntoView()
function, but I need to determine if the user was redirected to the page (which only happens on validation failure).
I attempted the following approach:
if (window.performance.navigation.type == 0) {
specificElement.scrollIntoView();
}
However, this condition will also be met if the user arrives after a form submission (which occurs right before - as there are two forms one after the other), rendering this solution ineffective.
Do you have any ideas on how to identify that the user was redirected?