Welcome to the nuBuilder Forums!
Register and log in to access exclusive forums and content available only to registered users.
Register and log in to access exclusive forums and content available only to registered users.
Readonly fields - prevent modification
Readonly fields - prevent modification
A read-only field can be changed with javascript/console. How do I prevent read-only fields from being modified or how to detect changes when/before saving a form? Can I retrieve somehow the value before the form was opened and compare it to the current value and abort the saving if the field value has changed?
Re: Readonly fields - prevent modification
Timo,
Good question.
That is what Before Save is for.
Users CAN change things in Javascript - if they know what they are doing.
But if you validate things on the server side using PHP, users cannot access the server and you can validate their changes using Hash Cookies.
https://wiki.nubuilder.cloud/ ... efore_Save
Then you can create your own error messages to users using nuDisplayError().
https://wiki.nubuilder.cloud/ ... splayError
Steven
Good question.
That is what Before Save is for.
Users CAN change things in Javascript - if they know what they are doing.
But if you validate things on the server side using PHP, users cannot access the server and you can validate their changes using Hash Cookies.
https://wiki.nubuilder.cloud/ ... efore_Save
Then you can create your own error messages to users using nuDisplayError().
https://wiki.nubuilder.cloud/ ... splayError
Steven
Re: Readonly fields - prevent modification
Thank you for the links, I don't find any information on the wiki for this:
pseudo code:
How would this be done?Can I retrieve somehow the value before the form was opened and compare it to the current value and abort the saving if the field value has changed?
pseudo code:
Code: Select all
foreach (...) {
if (field == readonly and field.oldvalue <> field.newvalue ) {
nuDisplayError(...)
}
}
Re: Readonly fields - prevent modification
Timo,
You can do this in Before Save.
https://wiki.nubuilder.cloud/ ... s#PHP_Code
With Hash Cookies.
https://wiki.nubuilder.cloud/ ... sh_Cookies
Steven
You can do this in Before Save.
https://wiki.nubuilder.cloud/ ... s#PHP_Code
With Hash Cookies.
https://wiki.nubuilder.cloud/ ... sh_Cookies
Code: Select all
$s = "SELECT * FROM product WHERE product = '#RECORD_ID#'";
$t = nuRunQuery($s);
$r = db_fetch_object($t);
//-- $r->pro_units is what it is now
//-- #pro_units# is what it has been changed into.
if($r->pro_units == '#pro_units#'){
nuDisplayMessage('Units must changed!');
}
Steven
Re: Readonly fields - prevent modification
Thanks for that! now I just need to find a way to loop through all read-only fields to detect a modification.
-
- Posts: 785
- Joined: Sun Oct 14, 2018 11:25 am
Re: Readonly fields - prevent modification
Retrieve the read-only fields of the current tableTimo wrote:I just need to find a way to loop through all read-only fields to detect a modification.
Code: Select all
function form_readonly_field_names()
{
$a = array();
$s = "SELECT `sob_all_id` FROM `zzzzsys_object` WHERE `sob_all_zzzzsys_form_id` = '#form_id#' AND `sob_all_access` = 1";
$t = nuRunQuery($s);
while ($r = db_fetch_row($t)) {
$a[] = $r[0];
}
return $a;
}
Code: Select all
$r = form_readonly_field_names();
$s = "SELECT * FROM `table_name` WHERE id = '#record_id#'";
$t = nuRunQuery($s);
while ($a = db_fetch_array($t)) {
foreach($a as $field => $val) {
// Check if it's a read-only field and if the old and new values don't match
if (in_array($field, $r) && $val != nuHash() ["$field"]) {
nuDisplayError("Field " . $field . " cannot be modified");
}
}
}