Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Forms & Blueprints

How to refocus on form if validation fails?

Solved by pamtbaau View solution

Started by jms 5 years ago · 3 replies · 499 views
5 years ago

My site is a one-page site based on the Agency theme. I have a Contact form at the bottom of the page. When you click the Submit button, if any of the field validators fail, it jumps to the top of the page. I'd instead like it to "autofocus" on the Contact form. Otherwise, I'm afraid the user will think their email got submitted when in fact it didn't. Is there any way to do this?

5 years ago

It might depend on a theme, but if it's a modular page, maybe you could change form action to the module URL, eg. http://mypage.tld/#form-module, or maybe implement AJAX form so that it would not reload the page

5 years ago Solution

@jmsstuff If you take a look at the Agency skeleton demo, you will see it jumps to the top of the form after validation.

Then if you take a look on Github at the form used by the contact module, it uses (as @Karmalakas suggested) the following form:

YAML
form:
    name: contact
    action: /home#contact
    ...etc
last edited 03/20/21 by pamtbaau
5 years ago

Thank you, I was missing that in my markdown. Now it works!

Suggested topics

Topic Participants Replies Views Activity
Forms & Blueprints · by Ton Haarmans, 5 years ago
13 1136 4 months ago
Forms & Blueprints · by Hugo Oliveira, 5 months ago
0 61 5 months ago
Forms & Blueprints · by Flachy Joe, 6 months ago
9 135 6 months ago
Forms & Blueprints · by Augustus, 7 months ago
7 110 7 months ago
Forms & Blueprints · by Julien, 7 months ago
10 129 7 months ago