XF 2.2 How to use CAPTCHA in an addon's template?

lobanz

New member
Hi! Having a grand time developing my first addon for XF 2. Can't find anything on these in the Community forums or the docs. If they ARE there, I'd appreciate a reference.

Need to use a captcha in a template. Didn't see any <xf:captcha> or macro available. Any guidance on how to best do this in XF and how to validate it in a Controller action? Any example code?

Also need to know if there is any XenForian way of implementing a Copy to Clipboard button.


Thanks!
 
There is actually a <xf:captcha> tag though we only ever use the <xf:captcharow> variant.

HTML:
<xf:captcharow label="{{ phrase('verification') }}" />

Then in your code you'll need something like:

PHP:
if ($this->isPost())
{
   if (!$this->captchaIsValid())
   {
      return $this->error(\XF::phrase('did_not_complete_the_captcha_verification_properly'));
   }

    // TODO: other code logic for POST request here
}

Also need to know if there is any XenForian way of implementing a Copy to Clipboard button.
There's a few different copy to clipboard features in the software. One example:

HTML:
<xf:button icon="copy"
   data-xf-init="copy-to-clipboard"
   data-copy-target=".js-copyTarget"
   data-success="Copied to the clipboard successfully"
   class="button--link is-hidden" />

data-copy-target is the selector of the element that contains the information you wish to copy. This can be an element like span div or code that contains the text you want to copy or an input or textarea element you wish to get the value from.

The class is-hidden is in case the browser doesn't support the relevant clipboard APIs. If they do, we remove that class.
 
HTML:
<xf:captcharow label="{{ phrase('verification') }}" />

Excellent! Thank you very much for y our help. I knew there was probably something.

Will this disable my submit button on the template until the captcha is solved?? How does that work? Or is it just handled on the server side with the $this->captchaIsValid()?
 
No but the form will error on submit if the captcha isn't completed - that's what the captchaIsValid call will do when you put it in your code. You just need to check that before you run any code that would make/save any changes.
 
No but the form will error on submit if the captcha isn't completed - that's what the captchaIsValid call will do when you put it in your code. You just need to check that before you run any code that would make/save any changes.

OK. I can't get the captcha to show. Any ideas? Using regular user (not admin) and captchas are enabled -- showed up on referral form. Is it because I don't have an action on my form yet?

HTML:
<xf:title>Some title</xf:title>

<xf:form>
  <div class="block-container">
    <div class="block-body">
      <xf:captcharow label="{{ phrase('verification') }}" hint="{{ phrase('required') }}"/>
      <div class="block-row">blah2</div>
    </div>
  </div>
</xf:form>
 
Ah, try:

HTML:
<xf:captcharow label="{{ phrase('verification') }}" hint="{{ phrase('required') }}" force="true" />

We don't usually show captcha to logged in users but you can force it with the force attribute.
 
Ah, try:

HTML:
<xf:captcharow label="{{ phrase('verification') }}" hint="{{ phrase('required') }}" force="true" />

We don't usually show captcha to logged in users but you can force it with the force attribute.


That did it! Where are the docs on all those <xf:things> ?
 
Top Bottom