Joomla 5 Mitteilung

Wir freuen uns mitteilen zu können, dass seit dem 29. Januar 2024 alle unsere Joomla Erweiterungen mit Joomla 5 kompatible sind.

Für alle die gerade noch von Joomla 3 auf 4 aktualisieren: Anleitungen für die Joomla 4 Migration gibt es hier:

Es gibt nun auch eine eigenständige Dokumentation für Visforms für Joomla 4 und für Visforms auf Joomla 5

Forum

Visforms Subscription Inhaber können in unserem Forum Fragen stellen. Bitte mit dem entsprechenden Benutzer anmelden.
Jeder kann lesend auf das Forum zugreifen.

Bitte stellen Sie nur 1 Frage pro Thema.

validate text field against database table

Mehr
7 Jahre 8 Monate her - 7 Jahre 8 Monate her #3425 von 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
Letzte Änderung: 7 Jahre 8 Monate her von nurd.

Mehr
7 Jahre 8 Monate her - 7 Jahre 8 Monate her #3428 von Administrator AV
Administrator AV antwortete auf 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 :-).
Letzte Änderung: 7 Jahre 8 Monate her von Administrator AV.
Folgende Benutzer bedankten sich: nurd

Mehr
7 Jahre 8 Monate her #3429 von 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".

Moderatoren: Administrator AVAdministrator IV
Powered by Kunena Forum