Welcome to the nuBuilder Forums!

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

object default value

Questions related to using nuBuilder Forte.
Post Reply
databs1234
Posts: 30
Joined: Wed Sep 06, 2023 12:45 am

object default value

Unread post by databs1234 »

I'm trying to have a the user field default to the current user that is logged into the system, the current date, the current time in fields when the add button is clicked. I found this on github:

https://github.com/nuBuilder/nuBuilder- ... ault_value

and tried the solutions without success. None of them load the current user, date, time into a field when the add button is clicked. I searched the forum and could not find anything. Is there another solution that provides this?

Thanks so much for your help.
kev1n
nuBuilder Team
Posts: 4292
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

Re: object default value

Unread post by kev1n »

Is it necessary to have this information on the form already when the record is added or is it enough to save the user and date details into the database when the record is saved?
databs1234
Posts: 30
Joined: Wed Sep 06, 2023 12:45 am

Re: object default value

Unread post by databs1234 »

preference would be to have the information displayed when a new records is being added in the form or grid view. example would be a comments subform with the user name, date, time fields auto filled with those default values. the user would see that they fields have already been filled and the focus would move the cursor to the comments fields so they would just need to type the comment.
databs1234
Posts: 30
Joined: Wed Sep 06, 2023 12:45 am

Re: object default value

Unread post by databs1234 »

any ideas for this one? thanks for the help.
nac
Posts: 115
Joined: Tue Dec 12, 2017 11:28 pm
Location: Aberdeen, UK
Has thanked: 9 times
Been thanked: 12 times

Re: object default value

Unread post by nac »

Hello databs1234,

If you want to show the values in a (potential) new record, you would probably need to set default values using JavaScript.
e.g. you could add something like this to the Edit form JS.

Code: Select all

    
    if (nuIsNewRecord()) {
        nuSetValue("user_id", nuUserId());
        nuSetValue("date_time_stamp", nuCurrentDateTime());
    }
This would set the default value of the object 'user_id' to the current user and the value of 'date_time_stamp' to the current date & time of the user's computer when a new record is added. This value would use the time zone of the client(user) rather than the server and, of course, they could differ. You could capture the time stamp when a record is created in the database layer by using some of the MySQL/MariaDB functions. Have a look at https://mariadb.com/kb/en/timestamp/

Depending on your version of MySQL, you could use something like this to keep a track of when a record was created and subsequently updated.

Code: Select all

ALTER TABLE tablename ADD COLUMN ts_created datetime DEFAULT CURRENT_TIMESTAMP;
ALTER TABLE tablename ADD COLUMN ts_updated datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP;
Finally, you could use the method described in https://wiki.nubuilder.cloud/index.php?title=Logging_Activity to let nuBuilder (via PHP) log this activity. The values would need to be extracted using the JSON functions of MySQL and transformed to something that makes more sense - the time is stored as an integer which can be decoded with the FROM_UNIXTIME function, e.g.

Code: Select all

SELECT FROM_UNIXTIME(JSON_EXTRACT(tablename_nulog, '$.edited.time')) as last_edited FROM tablename;
A few ideas to play around with...

Neil
databs1234
Posts: 30
Joined: Wed Sep 06, 2023 12:45 am

Re: object default value

Unread post by databs1234 »

Attached is the code and where it was placed, it did not put the date into the field, is there another location where it should be placed?
You do not have the required permissions to view the files attached to this post.
databs1234
Posts: 30
Joined: Wed Sep 06, 2023 12:45 am

Re: object default value

Unread post by databs1234 »

when working with a main form, the above solutions will work, however, when working with a subform, the below solution has been tested and works:

var df = "events_subform" + nuSubformRowNumber(this.id) + "event_date";
if (nuGetValue(df) == '') {
var today = nuCurrentDate('mm/dd/yy');
nuSetValue(df, today);
}

var df = "events_subform" + nuSubformRowNumber(this.id) + "street";
if (nuGetValue(df) == '') {
nuSetValue(df, nuUserId());
}

The above code is placed into an object of the subform, for this example is was the name of the event, "on change" is the event that is used, it will fire every time event_name is changed but will only set the values in the target fields when they are empty (= setting a default). A screen shot is attached.
You do not have the required permissions to view the files attached to this post.
kev1n
nuBuilder Team
Posts: 4292
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

Re: object default value

Unread post by kev1n »

A bit shorter (untested):

Code: Select all

const df = nuSubformRowObject(this.id, 'event_date');
if (df.value === '') {
   df.nuSetValue(nuCurrentDate('mm/dd/yy'));
}
capstefano
Posts: 21
Joined: Sat Jan 21, 2023 12:17 am
Has thanked: 19 times
Been thanked: 4 times

Re: object default value

Unread post by capstefano »

kev1n wrote: Thu Oct 26, 2023 7:18 am Is it necessary to have this information on the form already when the record is added or is it enough to save the user and date details into the database when the record is saved?
I have a similar question. I'd like to save the current zzzsys_user_id in a field of a new record, but I don't need the process to be visible to the user, saving in database is fair enough for my case. How I do accomplish? i know that the hash cookie #USER_ID# is always available, but can't figure out how to use it
kev1n
nuBuilder Team
Posts: 4292
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

Re: object default value

Unread post by kev1n »

Hi,

One possibility is to use the PHP (BS) event:

Code: Select all

$nuMainForm   = nuHash()['nuFORMdata'][0]->id;
nuSetNuDataValue($nudata, $nuMainForm, 'your_user_id', "#USER_ID#");
"your_user_id" must be a text object on your form. (you may set it to invisible)
Post Reply