Page 1 of 2

Subform not saving new record

Posted: Sun Mar 18, 2018 10:31 pm
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?

Re: Subform not saving new record

Posted: Mon Mar 19, 2018 12:14 am
by admin
TonyD,

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

Steven

Re: Subform not saving new record

Posted: Mon Mar 19, 2018 2:45 am
by tonyd
yes. And the form is linked to the table because I can scroll through the field names.

Re: Subform not saving new record

Posted: Mon Mar 19, 2018 3:34 am
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

Re: Subform not saving new record

Posted: Mon Mar 19, 2018 11:52 am
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.

Re: Subform not saving new record

Posted: Mon Mar 19, 2018 4:09 pm
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

Re: Subform not saving new record

Posted: Mon Mar 19, 2018 4:23 pm
by toms
Well, there error message says "mattersid is specified twice.". Can you resolve this?

Re: Subform not saving new record

Posted: Mon Mar 19, 2018 4:46 pm
by admin
toms,

I don't understand what you mean.

Steven

Re: Subform not saving new record

Posted: Mon Mar 19, 2018 6:00 pm
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?

Re: Subform not saving new record

Posted: Mon Mar 19, 2018 7:55 pm
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?