3

I'm trying to use JFormFieldCaptcha to work on my custom jForm. I managed to get the job done with registration and contact forms. However i want to build my own contact form which is based on an XML file somehow look like this:

<form>
    <fieldset addfieldpath="<path to JFormFieldCaptcha class>">
        <field 
             name="captcha" label="Captcha" description="COM_DEZTOUR_ORDER_CAPTCHA_DESC"
         type="text" validate="captcha"
        />
     </fieldset>
</form>

i cannot figure out why this code not working. Any help would be appriciated!

Duy Tran
  • 85
  • 1
  • 2
  • 9
  • 1
    there could be absolutely loads of reasons why this isn't working as Joomla is hugely sensitive to correct naming. Best advice I can give is debug and find $data->form. Find a place just before your form is loaded an back step to see where your form fields are fetched. Should find the error in there (or at least provide us with more info) – GWed Oct 16 '12 at 11:18

1 Answers1

9

In order to use Joomla ReCaptcha plugin -

1)Get recaptcha keys from http://www.google.com/recaptcha

2)Set these keys to recaptcha plugin and activate it if it's not.

3) Go to Global Configuration=>Site=>Default Captcha

and set "Default Captcha"=>"Captcha - ReCaptcha"

4)Create xml form instance which has your captcha field

$form   =& JForm::getInstance('myform','path/to/form/form.xml');

5)Create fields inside form-

$fieldSets = $form->getFieldsets();
foreach ($fieldSets as $name => $fieldSet) :
?>          
    <?php
    foreach ($form->getFieldset($name) as $field):
    ?>
        <p>
        <?php if (!$field->hidden) : ?>
        <span class="formlabel"><?php echo $field->label; ?></span> 
        <?php endif; ?>
        <span class="control"><?php echo $field->input; ?></span>
        </p>
    <?php
    endforeach;
    ?>          
    <div class="clr"></div>
<?php
endforeach;             

6)After form submission validate form-

$post   = JRequest::get('post');
jimport( 'joomla.form.form' );
$form   =& JForm::getInstance('myform','path/to/form/form.xml');
$res    = $form->validate($post);

XML form example-

<?xml version="1.0" encoding="utf-8"?>
<form
    addfieldpath="/administrator/components/com_franchise/models/fields">
    <fieldset name="information">        
         <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>

You can also try this- How to use joomla recaptcha plugin to my custom Module

Community
  • 1
  • 1
Irfan
  • 7,029
  • 3
  • 42
  • 57
  • This method works perfect with the reCaptcha v2. How about the latest Joomla! invisible reCaptcha plugin? I can't validate the form because it always returns `g-recaptcha-response` as a `null`. Thank you @Irfan. https://docs.joomla.org/J3.x:Google_ReCaptcha – Frostbourn Oct 02 '18 at 19:49
  • 1
    @Frostbourn: Did you check this answer? https://joomla.stackexchange.com/questions/16078/my-captcha-stopped-working-after-upgrading-to-joomla-3-5/ – Irfan Oct 03 '18 at 13:13