Custom validation rules
RSForm! Pro allows to add custom validation rules for the fields that allow validation rules.
Since version 1.51.0, RSForm!Pro allows you to add custom validation rules without modifying the validation.php file. In order to add a new validation rule you will have to create a customvalidation.php file under the following location:
/components/com_rsform/helpers/customvalidation.php
Using this file you will have to create a class that extends the "RSFormProValidations" class as in the following example:
<?php defined( '_JEXEC' ) or die( 'Restricted access' ); require_once dirname(__FILE__).'/validation.php'; class RSFormProCustomValidations extends RSFormProValidations { public static function validationTest($value, $extra = null, $data = null) { // The following makes sure the submitted value is "test" if ($value == 'test') { // Return true if the validation passed. return true; } else { // Return false if the validation didn't pass. return false; } } }
- The name of the function will be the name of the validation rule, as displayed when choosing it from the Validation Rule area.
- On success, the function should return true. On failure, the function should return false. So, if you want to fail validation, just return false.
Variables being used:
$value - Actual value being sent (ie. the value submitted in the form)
$extra - Unused
$data - An array of field properties (eg. $data['NAME'] will return the name of the field)
How to create a validation rule that checks email addresses from the #__users table
<?php defined( '_JEXEC' ) or die( 'Restricted access' ); require_once dirname(__FILE__).'/validation.php'; class RSFormProCustomValidations extends RSFormProValidations { // This checks if the email exists public static function emailExists($value, $extra = null, $data = null) { $db = Joomla\CMS\Factory::getDbo(); $db->setQuery("SELECT * FROM #__users WHERE email = " . $db->q($value)); $result = $db->loadResult(); // We have found an email address - pass if ($result) { return true; } else { return false; } } // This checks if the email does NOT exist public static function emailDoesNotExist($value, $extra = null, $data = null) { $db = Joomla\CMS\Factory::getDbo(); $db->setQuery("SELECT * FROM #__users WHERE email = " . $db->q($value)); $result = $db->loadResult(); // We have found an email address - fail if ($result) { return false; } else { return true; } } }
Extra:
the "Birthday Field" also provides some specific date related validation rules, which can be further extended in a similar manner:
- the datevalidation.php file which is located under the same path, can be extended by creating a new customdatevalidation.php file.
- following the above code example, you'll only need to replace:
a) /validation.php with /datevalidation.php
b) class RSFormProCustomValidations extends RSFormProValidations with class RSFormProCustomDateValidations extends RSFormProDateValidations
the "Birthday Field" also provides some specific date related validation rules, which can be further extended in a similar manner:
- the datevalidation.php file which is located under the same path, can be extended by creating a new customdatevalidation.php file.
- following the above code example, you'll only need to replace:
a) /validation.php with /datevalidation.php
b) class RSFormProCustomValidations extends RSFormProValidations with class RSFormProCustomDateValidations extends RSFormProDateValidations
28 persons found this article helpful.
You Should Also Read
Custom PHP code HOT |
Display PHP variables by default when form is shown HOT |
PHP Scripts HOT |
Controlling the calendar HOT |
How can I prevent the user from selecting a date in the calendar field HOT |