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.

Plugins

reCAPTCHA not valid when part of a modular page on Agency theme

form solved

Solved by pamtbaau View solution

Started by Rob Duncan 5 years ago · 10 replies · 1593 views
5 years ago

I can confirm that my form works with Captcha disabled.

I am using the Agency theme, with a modular one-page site. When I set up the form for reCaptcha, I get the following error in grav.log:

TXT
Form reCAPTCHA Errors: [/home] ["missing-input-response"] [] []

I created a separate, non-modular page with the same code, and the CAPTCHA succeeds. Here is the code:

YAML
title: 'Contact Form'
form:
    action: '/home#contact'
    name: contact
    fields:
        name:
            name: name
            label: Name
            classes: form-control
            placeholder: 'Enter your name'
            autocomplete: 'on'
            type: text
            position: left
            validate:
                required: true
        email:
            name: email
            label: Email
            classes: form-control
            placeholder: 'Enter your email address'
            type: email
            position: left
            validate:
                required: true
        message:
            name: message
            label: Message
            classes: form-control
            placeholder: 'Enter your message'
            type: textarea
            position: right
            validate:
                required: true
        g-recaptcha-response:
            name: g-recaptcha-response
            label: Captcha
            type: captcha
            recaptcha_site_key: 6xxxxxxxxxxxxxxxG9
            recaptcha_not_validated: 'Captcha not valid!'
    buttons:
        submit:
            type: submit
            classes: 'btn btn-primary btn-lg'
            value: Submit
    process:
        captcha: true
        message: 'Captcha passed...'
        save:
            fileprefix: contact-
            dateformat: Ymd-His-u
            extension: txt
            body: '{% include ''forms/data.txt.twig'' %}'
        email:
            subject: '[Site Contact Form] {{ form.value.name|e }}'
            body: '{% include ''forms/data.html.twig'' %}'
        display: null

the form.yaml is:

YAML
enabled: true
built_in_css: true
inline_css: true
refresh_prevention: false
client_side_validation: true
inline_errors: false
files:
  multiple: false
  limit: 10
  destination: self@
  avoid_overwriting: true
  random_name: false
  filesize: 0
  accept:
    - 'image/*'
recaptcha:
  version: '3'
  theme: dark
  site_key: 6LxxxxxxxxxxG9
  secret_key: 6LxxxxxxxxxxZhQ
5 years ago

@rcdncn, The only thing I can see at the front-end is that the modular page does not load the reCaptcha javascript files. And hence, no token will be send to the server which expects to receive one. The server then starts whining...

Haven't looked at your setup yet.

last edited 06/22/21 by pamtbaau
5 years ago

@rcdncn, The following setup seems to be working fine using a default install of the latest Grav and Quark, but fails to work using Agency.

Folder structure:

TXT
user/pages
├── 01.home
│   └── default.md
├── 02.typography
│   └── default.md
└── 03.contact
    ├── _contact
    │   └── form.md
    └── modular.md

Content of 'modular.md':

YAML
---
content:
  items: '@self.modular'

form:
  action: /contact
  name: my-nice-form
  fields:
    name:
      name: name
      type: text

    g-recaptcha-response:
      name: g-recaptcha-response
      type: captcha
      recaptcha_site_key: 'my site key'
      recaptcha_not_validated: 'Captcha not valid!'

  buttons:
    submit:
      type: submit

  process:
    captcha: true
    message: 'Thank you for your feedback!'
---

Content of 'form.md':

YAML
---
cache_enable: false
---

Content of '/user/config/plugins/form.yaml':

YAML
recaptcha:
  version: '3'
  site_key: 'my site key'
  secret_key: 'my secret key'

As said, this setup is working fine using Quark.

When switching to Agency:

  • The input fields are not being shown. Only the buttons.
  • Javascript for reCaptcha is not loaded
  • An error is logged by plugin Form:

    [2021-06-23 09:45:15] grav.WARNING: Form reCAPTCHA Errors: [/contact] ["missing-input-response"] [] []

  • Fix (partly): Theme Agency comes with its own form.html.twig template for modular pages. When disabling (renaming) that template, the reCaptche scripts are being loaded correctly and reCaptcha is working properly.
    Although the layout of the form is no longer in the Agency style...

So, it seems it's not a Form issue, but rather an Agency issue.

last edited 06/23/21 by pamtbaau
5 years ago

But your example is with form setup on modular page itself, not on a module. I think that's a huge drawback overall of using modular pages. There's simply no way to make them work without any workarounds if you want just a tiny bit more complex module.

In this case, if you decide not to use the module with form anymore, it would not be enough to delete that page, but you'd also have to remember to remove form config from modular too.

5 years ago Solution

@rcdncn, Downloaded the Agency skeleton and inspected its '/templates/modular/form.html.twig' template further.

It appears form fields require a value for 'position'. Fields without the 'position' value are being ignored by the template.

Adding the following to field 'g-recaptcha-response' solves the issue:

TXT
position: left  # or right

Added an issue for the theme to fix this.

👍 2
last edited 06/23/21 by pamtbaau
5 years ago

@pamtbaau:
Fields without the ‘position’ value are being ignored by the form template of Agency

:man_facepalming: They should implement the default

5 years ago

@Karmalakas, There is more to it then adding a default position. Not all fields require a position because some are not intended to being displayed, like: hidden, captcha, honeypot. You don't want to add extra divs that require space for these fields.

5 years ago

Didn't dig into their code, but it sounds like, if hidden field doesn't have a position, then it's also ignored :) Anyway, IMO missing position should not remove the field completely.

5 years ago

Thank you @pamtbaau, and @Karmalakas for the quick analysis! I added a position statement to g-recaptcha-response: and it works, though I agree that is pretty unexpected behaviour.

👍

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 43 1 week ago
Plugins · by Xavier, 4 weeks ago
2 54 4 weeks ago
Plugins · by Luka Prinčič, 7 years ago
3 1181 1 month ago
Plugins · by Sebastian van de Meer, 1 month ago
1 48 1 month ago
Plugins · by PIERROT Alain, 2 months ago
3 73 2 months ago