Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

Readonly fields - prevent modification

Questions related to using nuBuilder Forte.
Post Reply
Timo
Posts: 217
Joined: Thu Mar 15, 2018 9:26 pm
Has thanked: 1 time

Readonly fields - prevent modification

Unread post 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?
admin
Site Admin
Posts: 2814
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 25 times

Re: Readonly fields - prevent modification

Unread post 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
Timo
Posts: 217
Joined: Thu Mar 15, 2018 9:26 pm
Has thanked: 1 time

Re: Readonly fields - prevent modification

Unread post 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(...)
}

}
admin
Site Admin
Posts: 2814
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 25 times

Re: Readonly fields - prevent modification

Unread post 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
Timo
Posts: 217
Joined: Thu Mar 15, 2018 9:26 pm
Has thanked: 1 time

Re: Readonly fields - prevent modification

Unread post by Timo »

Thanks for that! now I just need to find a way to loop through all read-only fields to detect a modification.
admin
Site Admin
Posts: 2814
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 25 times

Re: Readonly fields - prevent modification

Unread post by admin »

.
toms
Posts: 785
Joined: Sun Oct 14, 2018 11:25 am

Re: Readonly fields - prevent modification

Unread post 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");
            }
    }
}
Timo
Posts: 217
Joined: Thu Mar 15, 2018 9:26 pm
Has thanked: 1 time

Re: Readonly fields - prevent modification

Unread post by Timo »

Thank you so much !
admin
Site Admin
Posts: 2814
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 25 times

Re: Readonly fields - prevent modification

Unread post by admin »

.
Post Reply