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.

Support

Substitute recaptcha in form

Started by elisabetta 6 years ago · 9 replies · 546 views
6 years ago

I'd like to use something different from re-captcha in my form, adding a simple question, like "how much is 3+2 ?"
Is there a system to add this to the code displayed in grav tutorial for creating forms ?
Thanks!

6 years ago

Thanks Ole, I've seen it, but I have no idea how to merge it in this code

YAML
    title:  'Contact Form'

form:
  fields:
    name:
      type: text
      label: Name
      validate:
        required: true
        message: Please enter your name!
    email:
      type: text
      label: Email
      validate:
        type: email
        required: true
        message: Please enter your email address!
    subject:
      type: text
      label: Subject
      validate:
        required: true
        message: Please enter a subject for your message!
    message:
      type: textarea
      label: Message
      validate:
        required: true
        min: 10
        message: Email message needs to be more than 10 characters long!

  buttons:
    submit:
      type: submit
      value: Send Email

  process:

    email:
      from: "{{ form.value.email }}"
      to: "{{ config.plugins.email.to }}"
      subject: "[Contact] {{ form.value.subject|raw }}"
      body: "{{ form.value.message }}<br /><br />{{ form.value.name }}<br />{{ form.value.email }}"
    message: 'Thank you from contacting us!'
    display: thankyou

At what point do I have to insert your code? And how can I validate it?

last edited 07/16/20 by elisabetta
6 years ago

Here's an example with it inserted into your form:

YAML
title: "Contact Form"
form:
  fields:
    name:
      type: text
      label: Name
      validate:
        required: true
        message: Please enter your name!
    email:
      type: text
      label: Email
      validate:
        type: email
        required: true
        message: Please enter your email address!
    subject:
      type: text
      label: Subject
      validate:
        required: true
        message: Please enter a subject for your message!
    message:
      type: textarea
      label: Message
      validate:
        required: true
        min: 10
        message: Email message needs to be more than 10 characters long!
    skyscraper:
      type: radio
      label: What is five times four?
      options:
        penguin: 12
        seagull: 16
        albatross: 20
      validate:
        required: true
        pattern: "^albatross$"
        message: Not quite, try that math again.
  buttons:
    submit:
      type: submit
      value: Send Email
  process:
    email:
      from: "{{ form.value.email }}"
      to: "{{ config.plugins.email.to }}"
      subject: "[Contact] {{ form.value.subject|raw }}"
      body: "{{ form.value.message }}<br /><br />{{ form.value.name }}<br />{{ form.value.email }}"
    message: "Thank you from contacting us!"
    display: thankyou

It's used as a normal field, and the name doesn't really matter - so I've just called it "skyscraper". As explained in the docs, the labels and values should not match, so it now asks the question "What is five times four?"

The underlying values bear no relation to the question, so even though the correct answer is obviously 20 - the underlying value associated with that label is "albatross". This is also what is validated against, by just checking if the chosen answer contains this string.

It's very naive, but will generally fool most spambots because they are submitting forms with available values. None of the labels match the values, however, and so bots with naive matching-techniques can't successfully submit with form with the given values in the form.

👍 1
6 years ago

Thanks a million Ole! I'll definitely give it a try, and I think that a lot of people around here can take advantage from a form template like yours 😉

👍 1
6 years ago

@OleVik
Hi Ole. I have another small problem. In the report of what the sender wrote, it is reported also the value inserted as reply to the test (in Bold in my example).
Example:
Here is the summary of what you wrote to us:

Name: john doe
Email: [email protected]
Message: test message do not reply
What is five times eight?: oklahoma
<a href='#' target='blank'>Please accept our privacy policy</a>: Yes

Is there a system to hide the chosen option?, perhaps using something like "test validated"?
Thanks!

6 years ago

Only by customizing the template that sends the email from within your theme, or form:fields:process:body in your FrontMatter. Does the latter still look like "{{ form.value.message }}<br /><br />{{ form.value.name }}<br />{{ form.value.email }}"?

6 years ago

This is the code I use after process:

TWIG
process:
    -
        email:
            from: '{{ form.value.email }}'
            to: '{{ config.plugins.email.from }}'
            subject: '[Contact] {{ form.value.name|e }}'
            body: '{% include ''forms/data.html.twig'' %}'

I forgot to say I use this form in Gantry 5 templates (zenith, koleti, helium). I don't know if the problem can be related to |e, maybe it should be |raw

6 years ago

Your theme would need to override that template referenced, or use a custom template like it, to customize what is sent. I don't think Gantry will affect that, but I don't use Gantry myself.

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
2 53 9 hours ago
Support · by Anna, 3 days ago
2 59 12 hours ago
Support · by Justin Young, 13 hours ago
1 30 13 hours ago
Support · by Duc , 1 week ago
2 65 5 days ago
Support · by Colin Hume, 1 week ago
2 56 5 days ago