Skip to content

Instantly share code, notes, and snippets.

@shamsbd71
Last active August 26, 2015 09:39
Show Gist options
  • Save shamsbd71/468b719151b6fef29c2c to your computer and use it in GitHub Desktop.
Save shamsbd71/468b719151b6fef29c2c to your computer and use it in GitHub Desktop.
Using ReCaptcha in Joomla Anywhere from XML and Validate it

Joomla ReCaptcha plugin

Steps to use Google ReCaptcha

  1. Get recaptcha keys from
https://www.google.com/recaptcha/intro/index.html
i. Register new site
ii. Collect Site key
iii. Collect Secret key
  1. Now go to Joomla Plugin manager and edit Captcha - ReCaptcha plugin, fill up the info and activate it if it's not already.
Captcha - ReCaptcha
  1. Apply Settings in Joomla. Go to
Joomla Site => Global Configuration => Site => Default Captcha
set "Default Captcha"  =>  "Captcha - ReCaptcha"
  1. Create xml form XML which has your captcha field
<field
  name="captcha"
  type="captcha"
  label="COM_CONTACT_CAPTCHA_LABEL"
  description="COM_CONTACT_CAPTCHA_DESC"
  validate="captcha"

/>

So your XML for gonna look like this

<?xml version="1.0" encoding="utf-8"?>
<form>
  <fieldset name="basic">        
    <field id="name"
      name="name"
      type="text"
      label="Name"
      description=""
      class="inputbox"
      size="30"
      default=""
      required="true"
    />  
  
    <field
      name="captcha"
      type="captcha"
      label="COM_CONTACT_CAPTCHA_LABEL"
      description="COM_CONTACT_CAPTCHA_DESC"
      validate="captcha"
    />
  
  </fieldset> 
</form>
  1. Now load you XML form with joomla JForm
$form = JForm::getInstance('customForm','path/to/form/form.xml',array('control' => 'jform'));
  1. Now load the form in your view file
$fieldSets = $form->getFieldsets();
foreach ($fieldSets as $name => $fieldSet) :
  foreach ($form->getFieldset($name) as $field):
    echo $field->getControlGroup();
  endforeach;
endforeach; 

or you can do it like this for extended styles

echo $form->getLabel('name');
echo $form->getInput('name')
  1. After form submission validate form, first get the data
$data   = $this->input->get('jform', array(), 'array');
$form   = JForm::getInstance('customForm','path/to/form/form.xml');
$result = $form->validate($data);
// Check for validation errors.
if ($return === false)
{
  // codes goes here
  $return = '';
  // Get the validation messages from the form.
  foreach ($form->getErrors() as $message)
  {
    $return .= $message->getMessage();
  }
  
  //now you can return error msg
  return $return;
}
// validation successful, do your job here
  1. you may need to include this code if your site fails to load jform class
jimport( 'joomla.form.form' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment