1

I'm working on a drupal 8 project, I've created a webform and trying to associate a captcha to my webform using captcha contrib module. I've a personalized template for my webform in which I render the webform fields. I've associate the captcha to the webform under /admin/config/people/captcha/captcha-points and test as anonymous user it is always unable to associate captcha to the webform.

  • Should I print captcha in my webform template file? if so how to do it?
  • Should I add an element Captcha to my webform fields? if so is that element by default use captcha contrib module settings?

Any help is appreciate.

UPDATING

See how to override webform tamplate here

2

To captcha protect a webform you NEED to add a captcha element to it. When captcha contrib module is enabled on your site the available captcha challenge types are available to be used on a per webform basis.

The template twig file is mostly for design puproses.

If the placement of captcha within the form is not to your liking you should copy the default webform template from /modules/contrib/webform/webform.html.twig to your theme /themes/custom/MYTHEME/templates/webform--WEBFORM_MACHINE_NAME.html.twig and perhaps place each element seperately.

The default webform.html.twig is

<form{{ attributes }}>
  {{ title_prefix }}
  {{ children }}
  {{ title_suffix }}
</form>

And in our own copy we should take out {{ children }} and replace with individual elements but also we have to make sure form ids and actions are included manually and it the final template should end up looking similar to this

<form{{ attributes }}>
  {{ title_prefix }}
  {{ element.elements.first_name }}
  {{ element.elements.last_name }}
  {{ element.elements.captcha }}

  {# necessary elements #}
  {{ element.form_build_id }}
  {{ element.form_id }}
  {{ element.actions }}

  {{ title_suffix }}
</form>

Where captcha for example is the machine name of the captcha field

  • yeah I've tried this solution and the captcha is rendered but I'm not sure if this added captcha field use exactly the captcha contrib module settings (web site key, private key..) ? – MiharbKH Oct 15 '18 at 11:26
  • not sure what the question is. – GiorgosK Oct 15 '18 at 11:40
  • does webform use the contrib module Captcha settings for the new created captcha element? – MiharbKH Oct 15 '18 at 11:57
  • 1
    You should have captcha module installed to add captcha field to webform, so the captcha field will not be available for webform otherwise. What settings do you mean in particular ? – GiorgosK Oct 15 '18 at 12:00
  • 1
    first try without custom webform please and report your findings ... but perhaps try a simpler captcha validation like MATH ? – GiorgosK Oct 15 '18 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.