Page 1 of 1
object default value
Posted: Thu Oct 26, 2023 6:24 am
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.
Re: object default value
Posted: Thu Oct 26, 2023 7:18 am
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?
Re: object default value
Posted: Thu Oct 26, 2023 3:27 pm
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.
Re: object default value
Posted: Mon Oct 30, 2023 7:56 pm
by databs1234
any ideas for this one? thanks for the help.
Re: object default value
Posted: Tue Oct 31, 2023 11:55 pm
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
Re: object default value
Posted: Wed Nov 01, 2023 6:15 pm
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?
Re: object default value
Posted: Wed Nov 01, 2023 10:09 pm
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.
Re: object default value
Posted: Thu Nov 02, 2023 5:51 am
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'));
}
Re: object default value
Posted: Wed Jan 31, 2024 6:24 pm
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
Re: object default value
Posted: Thu Feb 01, 2024 7:58 am
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)