Add form caching issue

I have an odd problem,

I need to generate and show a unique id on the add from before the record is initially saved. To do this I am using callback add form, this basically created a new id in a separate id table and then adds this id into the form field.

Problem is if you then save that record and then add another the add form opens and has not refreshed the field with a new id it seems to have cached the last one, but the callbackAddForm should run the code everytime the add form is opened new?

If I close the add form, refresh the page then add form again then it adds a new id to the field.

Am I going about this in the wrong way?

$crud->callbackAddForm(function ($data) {		
		
        $AppGIdsModel = new AppGIdsModel();

        $indata = [
            'g_id_used'	=> 1
        ];

        $AppGIdsModel->save($indata);
		
		$db = \Config\Database::connect();

		$builder = $db->table('app_g_ids');
		$builder->selectMax('id');
		
		$query = $builder->get();
		
		$row = $query->getRow();
		
		if (isset ($row)) {
			
			$data['record_patch_number'] = 'G' . $row->id;
		}
		
		return $data;
	});

Actually I even find this is not being updated everytime the add form is opened?!?!?!

It only updates the time after the webpage has been refreshed.

$crud->callbackAddForm(function ($data) {

	$data['san_created_date'] = date('d/m/Y H:i:s');

	return $data;
});

Hello @markab ,

Just a quick update on this one. So this was initially designed like that in order to not have a second request that it is not required to the server without any need. This worked fine till I have introduced the callbackAddForm . Nobody really mentioned that there is a problem with this cache until this post.

So… I thought about it and I believe that I should remove the cache completely as it doesn’t really optimise that much the add form. I will investigate and see if I can find a quick way to solve this issue.

I just wanted to update you first before my investigation so it will not drive you crazy :smiley:

Regards
Johnny

1 Like

Johnny,

Brilliant thanks for the reply, yes that will help me greatly, I have been trying this morning to find a way around it.

The cache really does not work for callbackAddForm.

No to be annoying but do you have a timescale for a possible fix/hack? I would like to go live with my crud in 2 weeks, this would solve my issues but if it is not possible I will have to think of some other way.

Thanks

1 Like

Hi Johnny,

Any thoughts on this, my whole implementation of GroceryCrud and how I need to use it is based on removing this caching issue with the callbacks. I need to be able to get current data rather than cached data for each callback action.

Thanks,
Mark

Hello @markab ,

Actually I am sorry but I can’t promise any dates for that. You have two options here:

Option 1: Download nightly build. I just gave you access to the nightly build version. You can download the nightly build from here: User's Page Login in case you don’t have access try to logout and login back in.

Option 2. Change the JavaScript caching value yourself

I can guide you through the change that you should apply.

Go to the file: assets/grocery-crud/js/build/grocery-crud-v2.9.2.35fcd68.js

At around line: 11869 or search for the string: actionAdd then change the line cache: true to cache: false

See also screenshot if that helps:

1 Like

Hi Johnny,

Thanks for this and I confirm this works for the actionAdd. I still see the issue with the edit/update form I need the date to be re-evaluated when using callbackEditForm and callbackBeforeUpdate.

Currently it is caching the value from the last page reload.

What would the fix for this be?

Thanks

Hello @markab ,

That’s very weird. First of all I can’t reproduce the issue that you are referring to and it doesn’t make sense to be honest to cache the previous data. I believe that the issue that you have is maybe unrelated with the form caching. The only caching that I had was in addForm.

Can you give us more information about the issue? Have you tried to have var_dump($data) just to check the result of the data that you would like to show?

Regards
Johnny

I think you are correct I just created a quick test table and tried again and I think it works fine. Sorry for the confusion!

Ok I see where I was getting confused it was actually the close modal on save on the edit form that was confusing things, I actually made another post on asking how to modify this behaviour but the part of that confused my testing here is that in the Edit form keeping the modal open after save does not refresh the edit form, so not a cache issue but a refresh issue.

In the Edit form when close modal on save is not checked then the modal stays open and refreshes the edit form. Now I think this is what it is supposed to do but it only refreshes the datagrid in the background. If you had a callbackBeforeUpdate which modifies a field before update and the modal is set to stay open it does not refresh the edit form with that change.

Oh ok. Now it make sense :grinning:. I will investigate and let you know.

Hey @markab ,

I am glad to inform you that I’ve fixed the issues that you were referring to. You can download the latest version of Grocery CRUD Enterprise (2.9.3) with the fixes here: User's Page Login

Regards
Johnny

Thanks! I will test it tomorrow!!

I tested this morning and now it is working perfectly. Many Thanks!

There is one other request I have in this similar area. It is concerning the add form and close modal on save when this is not checked.

Would it be possible to have a config option to change how the close modal on save check box works?

Close modal on save is checked, then the modal closes as current function.

Then a config option for the following.

Type 1 - Close modal on save is not checked, when the save button on the add form is pressed the record is saved then the modal stays open and then a new fresh add form is available to use.

Type 2 - Close modal on save is not checked (additional method), when the save button on the add form is pressed the record is saved then the modal stays open but then switched to the edit form for the same record.

This allows for initial record creation and follow up edits without closing the modal then opening separate as an edit.

Thanks,

i got same issue, and i don’t know how to solve

If you are a enterprise user you need to download at least 2.9.3 I think

1 Like

First of all I can’t reproduce the issue that you are referring to and it doesn’t make sense to be honest to cache the previous data. I believe that the issue that you have is maybe unrelated with the form caching. The only caching that I had was in addForm.

Thanks for this and I confirm this works for the actionAdd. I still see the issue with the edit/update form I need the date to be re-evaluated when using callbackEditForm and callbackBeforeUpdate.

Hey Guys, I’m having a similar issue with just simply trying to increment an ID from the last value in the database, it would seem that it only reads the database once at the initial page-load (for the add page) then doesn’t seem to read the DB after inserting something?
Once the frame reloads after inserting a new field I get the new entry, but if I go to add another field the callback just doesn’t update with the latest value ++ incremented.
Hopefully I’m not speaking gibberish here, it’s hard to explain.