Steps to use Google ReCaptcha
- Get recaptcha keys from
https://www.google.com/recaptcha/intro/index.html
i. Register new site
ii. Collect Site key
iii. Collect Secret key
- 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
- Apply Settings in Joomla. Go to
Joomla Site => Global Configuration => Site => Default Captcha
set "Default Captcha" => "Captcha - ReCaptcha"
- 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>
- Now load you XML form with joomla JForm
$form = JForm::getInstance('customForm','path/to/form/form.xml',array('control' => 'jform'));
- 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')
- 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
- you may need to include this code if your site fails to load jform class
jimport( 'joomla.form.form' );