Reputation: 596
When the validation(in laravel project) of file input fails the user gets redirected to the current page.
The problem is that the user has to scroll down first in order to see the error message and therefore what he did wrong.
It seems like this isn't quite the best ux I can think of.
So I thought it would be nice to redirect to the page and 'scroll down'(via javascript) right to where the user made the mistake;
I can do it with scrollIntoView()
, I just need to find out if the user got redirected to the page (what only happens when validation fails)
I tried with this:
if (window.performance.navigation.type == 0) {
specificElement.scrollIntoView();
}
But it will also be 0
if the user came there after a form submission (which happens right before - I have two forms after one another), so this 'solution' doesn't help me at all.
Any ideas how to find out that the user got redirected?
Upvotes: 0
Views: 65
Reputation: 71
i think it will be easier to check if there are validation error first.. so if exist run the javascript srollintoview..
in laravel 5.7 you can use
@if ($errors->any())
<script>
element.scrollIntoView();
</script>
@endif
Upvotes: 1