Like us!

Joomla!™ Disclaimer

Joomla!™ is a registered trademark of Open Source Matters.
Tekdi Web Solutions, tekdi.net and techjoomla.com are not affiliated to Joomla!, Open Source Matters or any of its subsidiaries.

Home Blogs Joomla Validating your Joomla forms with ease & without bloating your apps

Validating your Joomla forms with ease & without bloating your apps

Written by Parth Lawate Friday, 16 April 2010 14:43
Rate this item
(11 votes)

http://tekdi.net/media/k2/items/cache/e4c07973dbc8eb2f7380bdedc4201087_S.jpgRecently looking at some extensions, i realized that a whole lot of Joomla extensions use a lot of custom js to add validations to their forms. While this might be needed in some cases, 90% of the times you should be able to use the inbuilt Joomla validation available via the mootools validate.js . Working using in built Joomla functions will help keep you code free from excessive external js & reduce the bloat. A lot of times it will also help reduce JS conflicts.

Ironically i was prompted to write this blog post when we discovered one of our old extensions having this very issue ! While making the corrections i thought a small blog about using native Joomla validation might be helpful to a lot of devs.

So here is how you go about adding Joomla form validation...

Initiate the Joomla validate.js

In order to include validation, call the validate.js as shown below.

JHTML::_('behavior.formvalidation');

Identify the form that needs to be validated by giving it a class

The form that you need to validate needs to be given a class "form-validate"

<form id="myForm" class="form-validate" method="post"></form>

What can be validated ?

You can do all the standard validations needed.

  • required
  • validate-username
  • validate-password
  • validate-numeric
  • validate-email

You can also create custom validation handlers by using "validate-[custom]".

Here's an example :

<input type="text" name="qty" id="qty" size="30"  class="required validate-numeric"/>

This will validate the field for numeric values & also set it to required.

Setting Custom Handlers

In order to validate field types not supported like for date you can write your own handler for a class & call it after validate.js

Window.onDomReady(function() {
document.formvalidator.setHandler('date', function(value) {
regex=/^\d{4}(-\d{2}){2}$/;
return regex.test(value);
})
}

This can take care of the Javascript side validation. You can additionally add checks in the file that you are posting data to to make sure the form is validated.

defined( '_JEXEC' ) or die( 'Restricted access' );  //Verify Joomla enabled
$jAp=& JFactory::getApplication();
if ($_POST['check']!=JUtility::getToken()) {
// First verify (by a Javascript error or other methods) that the form has not been submitted without the validation
if ($_POST['check']=='post') $jAp->enqueueMessage('Please check all the fields of the form, aub.<br/>
If your browser blocks Javascript, then this form will never be successful. This is a security measure.'
,'error');
// If the check still isn't a valid token, do nothing. This might be a spoof attack or other invalid form submission
return false;
}

Reference : http://docs.joomla.org/Form_validation

BLOG COMMENTS POWERED BY DISQUS