Page 1 of 1

Default value for object

Posted: Tue Mar 06, 2018 2:04 am
by danpadro
In Nu3 I was set to default value

Code: Select all

SELECT CURDATE ();
or the current user

Code: Select all

SELECT '# nu_user_id #'
and at the opening of the form, we showed the current date or the current user.
For this:
nu31.jpg
Result
nu31.png
and
nu32.jpg
Result
nu32.png
what should i do in Nu4?

thanks.

Re: Default value for object

Posted: Tue Mar 06, 2018 11:26 pm
by danpadro
I've seen something here posted by toms but before I apply .... it's gone. :( anyway...
To get the current date in the form field I put this code in javascript...

Code: Select all

function mysqlDate(date){
    date = date || new Date();
    var dd = date.getDate();
    var mm = date.getMonth()+1;
    var yyyy = date.getFullYear();

if(dd<10) {
    dd='0'+dd
} 
if(mm<10) {
mm='0'+mm 
} 
return dd + "." + mm + "." + yyyy; //--ex. 07.03.2018
}
... then at the event in the object I put this (onfocus)

Code: Select all

$('#day_date').val(mysqlDate());
everything is ok, the field is populated but it is not saved. Save only if I click on the current calendar date.
any suggestion?

Re: Default value for object

Posted: Wed Mar 07, 2018 2:01 am
by admin
danpadro,

You are nearly there...

Code: Select all

$('#day_date').val(mysqlDate()).change();
nuBuilder only saves fields that have been edited - running change() sets the field property to edited.


Steven

Re: Default value for object

Posted: Wed Mar 07, 2018 2:38 am
by admin
danpadro,

Here is another alternative...

Code: Select all

var d = new Date();
var f = d.getFullYear() + '-' + nuPad2(d.getMonth() + 1) + '-' + nuPad2(d.getDate());
var n = $('#cus_date').attr('data-nu-format');
var v = nuFORM.addFormatting(f, n);

$('#cus_date').val(v).change();


Steven

Re: Default value for object

Posted: Wed Mar 07, 2018 4:10 am
by toms
danpadro,

Here is my answer again:

In nuBuilderForte you can set a field's default value with javascript. Check out this related thread:

https://forums.nubuilder.cloud/viewtopic. ... ult#p15692

Example:
nuuser_default.png
Let me know if you have any further questions.

Re: Default value for object

Posted: Wed Mar 07, 2018 4:49 pm
by danpadro
All 3 variants (2 Steven and 1 toms) we tested and worked perfectly !!!
I would come up with an 'IF' for version 2 (Steven) for those who do not want to add the date automatically when it exists (useful when the form exists and just edits). Thanks Steve and toms for help and work done.

Code: Select all

var d = new Date();
var f = d.getFullYear() + '-' + nuPad2(d.getMonth() + 1) + '-' + nuPad2(d.getDate());
var n = $('#cus_date').attr('data-nu-format');
var v = nuFORM.addFormatting(f, n);

if ( $('#cus_date').val() == '0' || $('#cus_date').val() == '' || $('#cus_date').val() == 'undefined' || $('#cus_date').val() == null )

$('#cus_date').val(v).change();

Re: Default value for object

Posted: Wed Mar 07, 2018 6:32 pm
by admin
.