Page 1 of 1

Readonly fields - prevent modification

Posted: Thu May 17, 2018 4:00 pm
by Timo
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

Posted: Fri May 18, 2018 4:15 am
by admin
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

Re: Readonly fields - prevent modification

Posted: Fri May 18, 2018 7:04 am
by Timo
Thank you for the links, I don't find any information on the wiki for this:
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?
How would this be done?

pseudo code:

Code: Select all

foreach (...) {

if (field == readonly and field.oldvalue <> field.newvalue ) {

 nuDisplayError(...)
}

}

Re: Readonly fields - prevent modification

Posted: Sat May 19, 2018 1:33 am
by admin
Timo,

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

Posted: Sat May 19, 2018 1:54 am
by Timo
Thanks for that! now I just need to find a way to loop through all read-only fields to detect a modification.

Re: Readonly fields - prevent modification

Posted: Mon May 21, 2018 12:41 am
by admin
.

Re: Readonly fields - prevent modification

Posted: Mon May 21, 2018 8:25 am
by toms
Timo wrote:I just need to find a way to loop through all read-only fields to detect a modification.
Retrieve the read-only fields of the current table

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;
}
Select the field names and values (before the form was saved)

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");
            }
    }
}

Re: Readonly fields - prevent modification

Posted: Wed May 23, 2018 2:53 pm
by Timo
Thank you so much !

Re: Readonly fields - prevent modification

Posted: Wed May 23, 2018 5:39 pm
by admin
.