add captcha to external form

Permalink
Hi there

After testing around for a while, I reached the point to ask you guys.

How the hack can I add a captcha validation to an external form?

myForm.php
$captcha = Loader::helper('validation/captcha');
<?php $captcha->display(); ?>
<?php $captcha->showInput(); ?>


This adds the Captcha. Great. But how to validate it?

In the Controller
$captcha = Loader::helper('validation/captcha');
echo "captcha: " . $captcha->check($_REQUEST['ccmCaptchaCode']);


This returns nothing. The Captcha is never validated...

Thanks a lot.

Steff
 
hutman replied on at Permalink Best Answer Reply
hutman
You should just need this in your controller

if (!$captcha->check()) {
     //error stuff goes here
}


It will find the correct captcha stuff in the post information and validate it.
Steff replied on at Permalink Reply
Steff
There must have been a typing error or somethink like this.

Thanks huntman. It works like a charm.
GrizzlyAdams replied on at Permalink Reply
Can u post what you have in your controller? I am unable to get this to validate the captcha
hutman replied on at Permalink Reply
hutman
What version of C5 are you using?
GrizzlyAdams replied on at Permalink Reply
5.7
hutman replied on at Permalink Reply
hutman
I have this in the controller

$captcha = Core::make("captcha");
if (!$captcha->check()) {
    $this->error->add('Incorrect Captcha');
}


and in the view I have

<div class="form-group">
    <label class="control-label"><?php echo $captcha->label(); ?></label>
    <div><?php $captcha->display(); ?></div>
    <div><?php $captcha->showInput(); ?></div>
</div>
GrizzlyAdams replied on at Permalink Reply
Hmm, I am getting a C5 error. Here's my action in the controller....

public function action_contact_us($bID = false)
    {
         $captcha = Core::make("captcha");
            if (!$captcha->check()) {
                $this->error->add('Incorrect Captcha');
            }
        if ($this->bID == $bID) {
         foreach($_POST as $key=>$value)
               {
                  if($key!=="send")
                  {
               $kkey = str_replace('_', ' ', $key);
                  if(is_array($value)){
                     $value = implode(', ', $value);
                  }
hutman replied on at Permalink Reply
hutman
What is the error you are getting?
GrizzlyAdams replied on at Permalink Reply 1 Attachment
I attached a screenshot. It seems that it's not even seeing the Captcha. If I take out the captcha, the form processes fine.
hutman replied on at Permalink Reply
hutman
2 things:
1) Do you have use Core; at the top of the controller?
2) Can you change your debug settings to see the actual error?
GrizzlyAdams replied on at Permalink Reply
I assume it doesn't matter the captcha? It's the "I'm not a robot" one. Also, I have $captcha = Loader::helper('validation/captcha'); in the top of the view.
GrizzlyAdams replied on at Permalink Reply
This is what's above my action in the top of the controller....
<?php
namespace Application\Block\ExternalForm\Form\Controller;
use Concrete\Core\Controller\AbstractController;
use Loader;
class ContactFormPopup extends AbstractController
{
hutman replied on at Permalink Reply
hutman
Remove the use Loader; and put in use Core; loader is deprecated and shouldn't be used anymore.
GrizzlyAdams replied on at Permalink Reply
Changed that and still got the error. Here's the debug....
Call to a member function add() on a non-object
/home/bankofel/public_html/application/blocks/external_form/form/controller/contact_form_popup.php
{
    public function action_contact_us($bID = false)
    {
         $captcha = Core::make("captcha");
            if (!$captcha->check()) {
                $this->error->add('Incorrect Captcha');
            }
hutman replied on at Permalink Reply
hutman
Ok, so the issue is that you don't have an error object to add an error to, the captcha is incorrect. This code should really be inside of your if, not before it, otherwise it's probably getting called on the second form submit.
GrizzlyAdams replied on at Permalink Reply
I don't get it, still not working :(

<?php
namespace Application\Block\ExternalForm\Form\Controller;
use Concrete\Core\Controller\AbstractController;
use Core;
class ContactFormPopup extends AbstractController
{
    public function action_contact_us($bID = false)
    {
        if ($this->bID == $bID) {
         $captcha = Core::make("captcha");
            if (!$captcha->check()) {
                $this->error->add('Incorrect Captcha');
            }         
         foreach($_POST as $key=>$value)
               {
hutman replied on at Permalink Reply
hutman
<?php
namespace Application\Block\ExternalForm\Form\Controller;
use Concrete\Core\Controller\AbstractController;
use Core;
class ContactFormPopup extends AbstractController
{
    public function action_contact_us($bID = false)
    {
        if ($this->bID == $bID)
        { 
            $captcha = Core::make("captcha");
            if ($captcha->check())
            { 
                foreach($_POST as $key=>$value)
                {
GrizzlyAdams replied on at Permalink Reply
Dang, was hopeful with that.
Whoops \ Exception \ ErrorException (E_RECOVERABLE_ERROR) 
Object of class Concrete\Core\Error\Error could not be converted to string
/home/bankofel/public_html/application/blocks/external_form/form/contact_form_popup.php
<div class="head_contact_form">
<div class="container">
<div class="row">
<div class="col-md-6 col-sm-6 col-xs-12 col-md-offste-3 col-sm-offset-3">
<div class="form_out">
<div class="alert alert-danger" style="display:none;"><?php echo $error?></div>
<?php if (isset($response)) { ?>
   <div class="alert alert-info"><?php echo $response?></div>
hutman replied on at Permalink Reply
hutman
Change this line

<div class="alert alert-danger" style="display:none;"><?php echo $error?></div>


to
<div class="alert alert-danger" style="display:none;"><?php $error->output(); ?></div>
GrizzlyAdams replied on at Permalink Reply
Man, didn't think this would be so darn difficult!

<?php $error->output(); ?> completely blows up my site :(
hutman replied on at Permalink Reply
hutman
What does "completely blows up my site" mean? Gives you an error? Gives you a white screen?
GrizzlyAdams replied on at Permalink Reply
It looks similar to the other error
Exception Occurred: /home/bankofel/public_html/application/blocks/external_form/form/contact_form_popup.php:13 Call to a member function output() on a non-object (1)


Weird that I can't load any page on the site either.
hutman replied on at Permalink Reply
hutman
I'm guessing you get this when there are no errors, try this

<?php if($error){ ?>
<div class="alert alert-danger" style="display:none;"><?php $error->output(); ?></div>
<?php } ?>
GrizzlyAdams replied on at Permalink Reply
Ok, well that got rid of that. However, I can still submit the form without checking the "I am not a robot" checkbox. That doesn't seem right...
hutman replied on at Permalink Reply
hutman
Can you just post ALL of your code so we can see what we're dealing with.
GrizzlyAdams replied on at Permalink Reply
Sure, here's the view code
<div class="head_contact_div"  style="display:none;"><?php
$form = Loader::helper('form');
$captcha = Loader::helper('validation/captcha');
defined('C5_EXECUTE') or die("Access Denied.");
?>
<div class="head_contact_form">
<div class="container">
<div class="row">
<div class="col-md-6 col-sm-6 col-xs-12 col-md-offste-3 col-sm-offset-3">
<div class="form_out">
<!--<div class="alert alert-danger" style="display:none;"><?php //echo $error?></div>-->
<?php if($error){ ?>
<div class="alert alert-danger" style="display:none;"><?php $error->output(); ?></div>
<?php } ?>
<?php if (isset($response)) { ?>


And here's the controller
<?php
namespace Application\Block\ExternalForm\Form\Controller;
use Concrete\Core\Controller\AbstractController;
use Core;
class ContactFormPopup extends AbstractController
{
    public function action_contact_us($bID = false)
    {
        if ($this->bID == $bID)
        { 
            $captcha = Core::make("captcha");
            if ($captcha->check())
            { 
                foreach($_POST as $key=>$value)
                {
hutman replied on at Permalink Reply
hutman
This code works fine for me if I remove the display: none; from the error->output div and the head_contact_div
GrizzlyAdams replied on at Permalink Reply
Thanks for all your help but it just isn't working. I have to have those hidden. The form is in a modal. I don't understand why it still allows the form to submit if the captcha fails.
GrizzlyAdams replied on at Permalink Reply
Me too.

I am wondering if maybe an AJAX submission would be better since this is in a modal? Problem is that on submit, the modal is closing before the captcha has a chance to validate. Any grand ideas about this?
ob7dev replied on at Permalink Reply
ob7dev
When the form gets posted the page reloads, meaning your modal is gone? If so then perhaps AJAX is the way to go.
Khurramali replied on at Permalink Reply
Khurramali
Thanks for sharing. This really helped.