Tuesday, July 16, 2013

How to use capture in custom joomla form

Let’s see how to add capture to custom form.I’m presenting this based on my previous posts about Joomla Frontend Custom Form Developing (Step 1, Step 2, Step 3).
There are several plugging for joomla capture.I’m using Joomla ReCaptcha plugin to my development.It’s available from joomla 2.5.
First of all enable recapture plugging on joomla plugging manager.Then Get recaptcha keys from http://www.google.com/recaptcha.To get those keys you need to have google account.Go to Recapture and Set these keys to recaptcha plugin as bellow.
ReCapture
Then Go to Global Configuration=>Site=>Default Captcha and set "Default Captcha"=>"Captcha - ReCaptcha".
Add following code to components\com_myresourcelist\models\forms\joinwithus.xml.
<field name="captcha" type="captcha" label="Captcha"
            description="Type Captcha Text" validate="captcha" />
Then your code will be as follows.
 

Add captcha to view by adding following code to components\com_myresourcelist\views\joinwithus\tmpl\default.php
<dt><?php echo $this->form->getLabel('captcha'); ?></dt>
<dd><?php echo $this->form->getInput('captcha'); ?></dd>
<dt></dt><dd></dd>
Now your form contain capture as follows.
image
It’s not finished still you can submit form with out capture validation.
Let’s see how to validate the capture.
Change submit method of components\com_myresourcelist\controllers\joinwithus.php as follows.it validate the form data.
Now it’s validating data and wrong capture directed to form again but we’ve lost all previously entered data.
image
The problem is we haven’t handle the user state.Let’s solve this now.In model class loadFormData() must override. This is a Method to get the data that should be injected in the form.
Suppose you need to clear email field after wrong submission.Add following code to get data method of a model class.
// Unset the Email.
unset($this->data->email);
I’ve added it to the git code above.
If something is wrong let’s share our knowledge.Keep a comment. 

No comments:

Post a Comment