Joomla 5 Notice

We are pleased to announce that as of January 29, 2024, all of our Joomla extensions are compatible with Joomla 5.

For all who are still updateing from Joomla 3 to Joomla 4: Joomla 4 Migration instructions are available here:

There is now a separate Documentation for Visforms for Joomla 4 and for Visforms for Joomla 5!

Forum

Visforms Subscription user can ask questions in our forum. Please log in with the relevant user first.
Everybody can access the forum for reading.

Please only ask 1 question per topic.

validate text field against database table

More
7 years 8 months ago - 7 years 8 months ago #3425 by nurd
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
Last edit: 7 years 8 months ago by nurd.

More
7 years 8 months ago - 7 years 8 months ago #3428 by Administrator AV
Replied by Administrator AV on topic validate text field against database table
Hi,

thanks for this post and sharing your information with all Visforms users. That is great!

I just wanted to point out one suggestion.
The field name (visfield.xml) should not contain the string "attribute".
The string "attribute" in the field name will indicate for Visforms that this field represents an HTML attribute, which it certainly does not. This may cause problems with some other Visforms features (like multi page forms, conditional fields). So a better field name would be f_text_checkdb.

As this code changes Visforms core files, the changes will be lost with every update, which your are certainly aware of, but maybe not any Visforms user.

Good luck and kind regards,
Aicha

:idea: I recommend you the new and up-to-date documentation for Joomla 4:
docs.joomla-5.visforms.vi-solutions.de/en/docs/
Most of this also applies retrospectively to Joomla 3.
Please only ask 1 question per topic :-).

:idea: Ich empfehle Dir die neue und aktuelle Dokumentation für Joomla 4:
docs.joomla-5.visforms.vi-solutions.de/docs/
Das meiste gilt rückwirkend auch für Joomla 3.
Bitte immer nur 1 Frage pro Thema stellen :-).
Last edit: 7 years 8 months ago by Administrator AV.
The following user(s) said Thank You: nurd

More
7 years 8 months ago #3429 by nurd
thanks for your suggestion, I'll chenge that in my code. I just coppied the code for "required" checkbox and changed "_required" to "_checkdb".

Moderators: Administrator AVAdministrator IV
Powered by Kunena Forum