Do not output tag in Row_Inserting/Updating/Inserted/Updated server event (unless you need to do some workaround). If you must use server side validation:If you have enabled the advanced setting Use Bootstrap Toast message, you may just use $this->setFailureMessage(“my error message”), the error will be shown as a toast.If you have not enabled the advanced setting Use Bootstrap Toast message, the error message will be shown as an inline message above the form.If you need to output the error message by SweetAlert2, you may use, e.g. (Assume v2021, there is no need to add sweetalert.min.js yourself.)
// Row Inserting event
function Row_Inserting($rsold, &$rsnew)
{
WriteJson(["error" => "my error message"]); // Write error message
$this->terminate(); // Terminate the page
return false; // Set return value to false
}
If you want to display the error message by using SweetAlert in v2021, then you should use Form_CustomValidate under the Client Scripts section that belongs to the Add/Copy page (equivalent to Row_Inserting server event).
Just call ew.alert() Javascript global function that used by PHPMaker 2021 Javascript Framework, after that return false.For example, from orderdetails table in demo2021 project, just insert the following code in Form_CustomValidate under Client Scripts → Table-Specific → Add/Copy Page, just right before return true line:
// Your custom validation code here, return false if invalid.
var $qty = $(this).fields("Quantity"); // Get a field as jQuery object by field name
if ($qty.toNumber() % 10 != 0) {
ew.alert("Order quantity must be multiples of 10", "", "danger"); // display the error message
return false;
//return this.addCustomError("Quantity", "Order quantity must be multiples of 10."); //use this if you want to display the error message below the Quantity field
}
So, the complete generated code in that Form_CustomValidate should be like this:
// Form_CustomValidate
forderdetailsadd.customValidate = function(fobj) { // DO NOT CHANGE THIS LINE!
// Your custom validation code here, return false if invalid.
var $qty = $(this).fields("Quantity"); // Get a field as jQuery object by field name
if ($qty.toNumber() % 10 != 0) {
ew.alert("Order quantity must be multiples of 10", "", "danger");
return false;
//return this.addCustomError("Quantity", "Order quantity must be multiples of 10."); //use this if you want to display the error message below the Quantity field
}
return true;
}
thx! @mobhar Excellent thank you very much!!! for something simpler like add, edit and delete. Replaces the default one? If you don’t mind, could you replicate an example? Thanks a lot!!!