Echoing
<script> in a backend hook
(before_insert) is unreliable. It may not render properly, especially if the insert is triggered via AJAX or API. Instead try the following:
Code: Select all
function form_visita_before_insert(&$data, $memberInfo, &$args) {
$visitante = makeSafe($data['number']);
$fecha = makeSafe($data['date']);
$exists = sqlValue("SELECT COUNT(*) FROM form_visita WHERE number='{$visitante}' AND date='{$fecha}'");
if($exists > 0) {
WindowMessages::addDismissable(
"This visitor number was already captured today. Please verify before proceeding.",
"alert alert-warning"
);
// Optionally block the insert by uncommenting the following line, this will make sure no duplicate numbers are saved:
// return false;
}
return true;
}
Optional Client-Side Validation
In AppGini 24.18 and above
{tablename}_customValidateData will automatically be called during detail view form submission and if it returns false, the form will not be submitted. You can define a custom validation function in any of the following locations:
- hooks/header-extras.php
- hooks/footer-extras.php
- {tablename}-dv.js
- {tablename}_dv() hook function in hooks/{tablename}.php
Code: Select all
function form_visita_customValidateData(insertMode) {
const visitante = $j('#number').val();
const fecha = $j('#date').val();
let result = false;
$j.ajax({
url: 'hooks/check_duplicate_visit.php',
type: 'POST',
data: { number: visitante, date: fecha },
async: false,
success: function(response) {
if(response.exists) {
AppGini.modalError('This visitor number was already captured today.');
result = false;
} else {
result = true;
}
}
});
return result;
}
hooks/check_duplicate_visit.php
Code: Select all
<?php
include(dirname(__FILE__) . '/../lib.php');
$number = makeSafe($_POST['number']);
$date = makeSafe($_POST['date']);
$exists = sqlValue("SELECT COUNT(*) FROM form_visita WHERE number='{$number}' AND date='{$date}'");
echo json_encode(['exists' => ($exists > 0)]);