hello,
I've been looking for a solution where a text field could be validated against a list of possiblities in a database table.
there's no joomla extension that could do that..
lucky I could understand the code of visforms enough to come up with a solution
I'd like to leave it here if some one needs it, feel free to use it however you like.
first, add a checkbox to the text field creation form
in file \admin\models\forms\visfield.xml "fieldset name="visf_text" add the following:
Code:
<field name="f_text_attribute_checkdb" type="checkbox" label="COM_VISFORMS_DB_CHECK"
description="COM_VISFORMS_DB_CHECK_DESC" value="check"
/>
add label and description in the appropriate language file for example: \admin\language\en-GB\en-GB.com_visforms.ini
Code:
COM_VISFORMS_DB_CHECK="customsn.php check"
COM_VISFORMS_DB_CHECK_DESC="Check the box, if you want to validate against customsn.php"
now in \site\lib\business\text.php somwhere in between the validation if's add a new one
Code:
if ((isset($this->field->attribute_checkdb)) && ($this->field->attribute_checkdb == true))
{
include 'checksn.php';
if ( validateScript($this->field->attribute_value)== false) {
$valid= false;
$error = JText::sprintf('COM_VISFORMS, $this->field->label);
//attach error to form
$this->setErrors($error);
}
}
and finally, the "checksn.php" file is just a function doing whatever you want and returning true or false. in my case it's a database querry, but you can use it for any other purpouse. for example, it could check a license key against a key generation formula, or a coupon code and so on.
Code:
<?php
function validateScript($sn)
{
$servername = "localhost";
$username = "xxxx";
$password = "xxxx";
$dbname = "xxxxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT sn FROM custom_sn WHERE sn='".$sn."';";
$result = $conn->query($sql);
if ($result->num_rows == 0) {
//echo "not OK";
return false;
} else {
// echo "OK";
return true;
}
$conn->close();
}
?>
I hope some one will find this usefull