Welcome to the nuBuilder Forums!

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

Subform not saving new record

Questions related to using nuBuilder Forte.
tonyd
Posts: 68
Joined: Sun Mar 04, 2018 6:38 pm

Subform not saving new record

Unread post by tonyd »

I have a subform that is saving fine when I edit any of the current records in the underlying table, but does not save the new record/row when my Input:file object is dirtied in the new row. I have a nuBeforeSave function

Code: Select all

function nuBeforeSave(){

    $('[data-nu-table="Docs"]').each(function(index,item){
        if (item.children[2].classList.contains("nuEdited") && item.children[2].value !== ''){ //someone dirtied the Input:file object

            //console.log(item);
            
            var j = JSON.parse(item.children[2].value);     //textarea field of Input:file object
            var d = Date.now();

            item.children[1].value = item.attributes['data-nu-foreign-key'].value; //holds hidden mattersid
            item.children[1].classList.add('nuEdited');
            item.children[4].value = Date(d);               //'Last Modified' field
            item.children[4].classList.add('nuEdited');
            item.children[5].value = j.name;                //'File Name' field
            item.children[5].classList.add('nuEdited');
        }
    });

    return true;

}
As you can see I have added the 'nuEdited' class to every field that I have modified. What confuses me is the fact that editing a current row saves just fine. It only fails to save new rows.

What am I missing? Will a save fail if I miss adding the 'nuEdited' class to one of the fields?
TonyD
admin
Site Admin
Posts: 2814
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 25 times

Re: Subform not saving new record

Unread post by admin »

TonyD,

This is a bit of a guess but has the Subform's Form got the right Primary Key?

Steven
tonyd
Posts: 68
Joined: Sun Mar 04, 2018 6:38 pm

Re: Subform not saving new record

Unread post by tonyd »

yes. And the form is linked to the table because I can scroll through the field names.
TonyD
admin
Site Admin
Posts: 2814
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 25 times

Re: Subform not saving new record

Unread post by admin »

TonyD,

OK, my second guess is... instead of adding the class nuEdited to the Object, try $('#objectid').change();

Because an Input:File is treated (and saved) differently than all other Objects.


Steven
tonyd
Posts: 68
Joined: Sun Mar 04, 2018 6:38 pm

Re: Subform not saving new record

Unread post by tonyd »

Changed the code to use the jquery change() function.

Code: Select all

function nuBeforeSave(){

    $('[data-nu-table="Docs"]').each(function(index,item){
        if (item.children[2].classList.contains("nuEdited") && item.children[2].value !== ''){

            var ma   =   item.children[1].id;
            var da   =   item.children[4].id;
            var fn   =   item.children[5].id;
            var j = JSON.parse(item.children[2].value);
            var d = Date.now();

            $('#' + ma).val(item.attributes['data-nu-foreign-key'].value).change(); //holds mattersid
            $('#' + da).val(Date(d)).change();               //'Last Modified' field
            $('#' + fn).val(j.name).change();                //'File Name' field
        }
    });

    return true;

}
FYI, I have changed the nuChangeFile() function to save the file to the server instead of the database:

Code: Select all

function nuChangeFile(e){

	if(e.target.id.substr(-8) == 'nuDelete'){
		
		nuHasBeenEdited();
		return;
		
	}

	var theFile			= e.target.id;
	var theTextarea		= theFile.substr(0, theFile.length - 5);
	
	if($('#' + theFile).val()==''){return;}
    
	var a		= $('#' + theFile)[0].files[0];
	var r		= new FileReader();

	r.onload 	= function(e) {
	    
		var f = r.result; //don't really need this because we no longer
											//save file contents to the database
		var o = {'file' : '', 'name' : a.name, 'size' : a.size, 'type' : a.type};
		var j = JSON.stringify(o);
		
		$('#' + theTextarea).val(j).addClass('nuEdited');

		// create a form and load our file onto it
		var formdata = new FormData();
		formdata.append("files[]", a);

		$.ajax({										//-- save file to server
				url: "/nuuploader.php",
				type: "POST",
				data: formdata,
				processData: false,
				contentType: false, // this is important!!!
				success: function (D,S,T) {
					// should do some error checking here
					// reset formdata
					formdata = false;
				},
				error: function (X,S,T) {
					
					alert("Upload Failed: " + X);
				}
		});
	}

	r.readAsBinaryString(a);	//-- use a binary read instead of text
	
	var t	= $('#' + theFile)[0];
	var p	= $('#' + theTextarea).attr('data-nu-prefix');
	
	$('#' + p + 'nuDelete').prop('checked', false);
	$('#' + theTextarea).addClass('nuEdited');

	nuHasBeenEdited();
	
	if(p == ''){return;}

	nuAddSubformRow(t, e);
	
}
I get this nuDebug error message
error.jpg
I can see the field values change briefly in the subform before the save, but then the new fields goes blank again. I think the code looks cleaner this way, but it still doesn't work.
You do not have the required permissions to view the files attached to this post.
TonyD
admin
Site Admin
Posts: 2814
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 25 times

Re: Subform not saving new record

Unread post by admin »

TonyD,

The reason nuBuilder Forte has an Input:File Object is to allow simple things like
  • Passport photos.
    Pictures of products
    Company logos.
to be used to display things on Forms and Reports.

When a file is selected with Input:File, nuBuilder converts it to a JSON string.

This can take a while and is not an efficient way of handling big files.

If you want to handle bigger files you'll need to use something like Dropbox. - (we will post an example of how at some point).

If you want to change a nuBuilder function to change the way this works, you'll need to fork nuBuilder


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

Re: Subform not saving new record

Unread post by toms »

Well, there error message says "mattersid is specified twice.". Can you resolve this?
admin
Site Admin
Posts: 2814
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 25 times

Re: Subform not saving new record

Unread post by admin »

toms,

I don't understand what you mean.

Steven
tonyd
Posts: 68
Joined: Sun Mar 04, 2018 6:38 pm

Re: Subform not saving new record

Unread post by tonyd »

Steven,

I already modified nuBuilder (sorry, not sure how to "fork" yet) to handle larger file sizes by saving the files to the server instead of saving the large JSON object to the database, the code for it is shown in my previous post. The core nuBuilder code remains the same with only a minor modification to the JSON object representing the file before the JSON is saved to the database.


toms,

I cleaned up the zzzzsys_object table which had a bunch of unused objects with identical names from when I cloned some of the forms, but that did not seem to help. What I don't understand is how nuBuilder could write a query with the same field twice? I have looked in the database tables for any strange-looking duplicates, but found none. Does nuBuilder build queries from a form's objects list?
TonyD
toms
Posts: 785
Joined: Sun Oct 14, 2018 11:25 am

Re: Subform not saving new record

Unread post by toms »

TonyD, you can easily check the fields on your form by running this in the console:

Fields of the main form:

Code: Select all

nuSubformObject('').fields
Then do the same for your subform:

Code: Select all

nuSubformObject('your_sub_form_name').fields
Do you spot any duplicate fields?
Post Reply