Page 1 of 1

[JavaScript] nuBeforeSave (two issues)

Posted: Mon Jan 22, 2018 4:33 pm
by pyro
Hi,

I have a very strange behavior in my code.

If I add nuBeforeSave (even empty) it my page does not save
If I remove nuBeforeSave it does safe but sn_datetime is just 0000-00-00 00:00:00 whereas if I use nuBeforeSave it fills the correct date (e.g 2018-01-22 14:32:36).

Maybe also worth mentioning is if I have the "0000-00-00 00:00:00" issue it actually shows me the correct date in the field for about half a second before changing back to "0000-00-00 00:00:00" and save it.

I am sure the are two diffent issues, however they seem to be linked in my case.


Code: Select all

function nuOnClone() {
     $('#sn_sn').val('');
     $('#sn_sn').focus();
}

function checkchange() {
if ($('#sn_sn').val().length != 10) {   
     alert('This is not a Serial Number');
     $('#sn_sn').val('');
     return;
}

now = new Date().toISOString().slice(0, 19).replace('T', ' ');
$('#sn_user').val(nuAccessLevelCode());
$('#sn_datetime').val(now);

nuSaveAction();
setTimeout(function(){ nuCloneAction(); }, 2000);

}

function nuBeforeSave(){
}
checkchange if called as an onchange event
same layout as -> https://forums.nubuilder.cloud/viewtopic.php?f=19&t=9243
I just update unBuilder today from git hub.

thank you for your great help so far
BR
Moritz

Re: [JavaScript] nuBeforeSave (two issues)

Posted: Mon Jan 22, 2018 4:57 pm
by toms
Moritz,

I'm not sure what the default return value of the nuBeforeSave() function is.
Does it help adding

Code: Select all

 return true;
in that function?

Re: [JavaScript] nuBeforeSave (two issues)

Posted: Tue Jan 23, 2018 12:14 am
by pyro
Hi Tom,

thank you that did the trick.

Now there is just the issue with with the date field left if I do not call the nuBeforeSave funktion. I it seems to be the same issue as https://forums.nubuilder.cloud/viewtopic.php?f=19&t=9256

Re: [JavaScript] nuBeforeSave (two issues)

Posted: Tue Jan 23, 2018 1:47 am
by admin
pyro,

Here is a Input:nuDate Object...
date_format.PNG
To manually add a date you will need to add it in the format that the Object has been set to.

eg.

Code: Select all

$('#sn_datetime').val('2014/Feb/11').change();
Steven

Re: [JavaScript] nuBeforeSave (two issues)

Posted: Tue Jan 23, 2018 3:50 am
by pyro
Hello Steven,

$('#sn_datetime').val(now').change();

Helped me a lot thank you. However there still seems to be wired behavior about the datetime.
It seems that I need $('#sn_datetime').val('0000-00-00 00:00:00').change(); to initialize the field. If I don't use it the first time I save the form it will change the field back to "0000-00-00 00:00:00". But for further submits it will work.

It only works reliable when I use the following code:

Code: Select all

function nuOnClone() {
     $('#sn_sn').val('');
     $('#sn_sn').focus();
}

function checkchange() {
if ($('#sn_sn').val().length != 10) {   
     alert('This is not a Serial Number');
     $('#sn_sn').val('');
     return;
}

now = new Date().toISOString().slice(0, 19).replace('T', ' ');

if (nuAccessLevelCode()==='') {   
$('#sn_user').val('admin').change();
} else {
    $('#sn_user').val(nuAccessLevelCode()).change();
}

$('#sn_datetime').val('0000-00-00 00:00:00').change();
$('#sn_datetime').val(now).change();

nuSaveAction();
setTimeout(function(){ nuCloneAction(); }, 1000);

}

function nuBeforeSave(){
if ($('#sn_sn').val().length != 10) {   
     alert('This is not a Serial Number');
     $('#sn_sn').val('');
     return;  
}

return true;
}

my database and nuBuilder setting:
datetime nubuilder.PNG
datetime phpmyadmin.PNG
Thank you

Re: [JavaScript] nuBeforeSave (two issues)

Posted: Tue Jan 23, 2018 12:52 pm
by toms
pyo,

Would it work with a nuDate Input Type?

Re: [JavaScript] nuBeforeSave (two issues)

Posted: Tue Jan 23, 2018 4:30 pm
by admin
pyro,

If you do what toms said, it should work fine. It did here for me...
date_format.PNG
Steven