• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Page caching and validation_errors

Hi forum.

I have a question regarding how page caching and validation_errors work.

I have recently started to use caching (via $this->output->cache(n)Wink on my website. Some of the pages that utilize caching also have a form the user must fill out. If errors are encountered on submission then the validation_errors are echoed. I can see that the cached file does not include any validation errors in the html. And as I understand it, if a page is cached it should display only the html that is represented in the cached file when a given url is accessed. However, if a user improperly fills out the form, and thus populating validation_errors, the html being displayed DOES include the validation errors, despite the fact that the html for this url is cached.

Can anybody shed some light on why this is happening?



If the output class finds a cache file, but it is expired, a new one will be generated. If the new one happens to include the validation errors, then that HTML will also be included in the cache file.

I'd suggest checking if POST values have been submitted. If not, then retrieve a cache file. If so, the page will be generated normally.

Thanks for the response , Aken.

I guess I still am confused. The unexpired cached file does not include any validation errors in its html. So how is it possible that validation_errors are being displayed when the form is posted and errors are found? Shouldn't the html from unexpired cached file be displayed (i.e. without the validation_errors)? To the contrary, it seems as though the cached file is being ignored and the page regenerated as if caching was not enabled.

To those that are still interested. What I have found is that output caching will only work when you are using a simple view like
. As soon as you add any dynamic data to the view
codeigniter's output caching will not work properly. It seems as though views that use dynamic data need to use another caching library, such as the one here from phil sturgeon, that supports partial caching. Hopefully this post can help someone else save some time in the future.

That's not true. The Output class checks for and displays a cache file well before a controller is executed (in fact, if a valid cache file is found, the script stops dead after it is displayed).

After you've submitted your form, does your URL change? The Output class' cache detection goes by the URI (anything after your mysite.com name). If the URI is different, and no cache file exists or it is expired, a new one will be created and loaded on the next request to that URI (regardless of what data is posted to it).

When it comes to pages with forms and other dynamic info like that, I'd recommend using something that can do partial caching. CI's cache driver is one solution, or third party libraries like you mentioned. The Output class' cache isn't flexible enough.

I'd recommend going through the core files and seeing how caching works with the Output class, just for your own future reference. Smile

Aken is correct on how caching works.

When a cache file exists, CI doesn't even really process the request (except to see if the cached file's time has expired). It outputs the cache file and doesn't even load the controller (so it will never know if the dynamic data changed). When a request is made and it checks the cache files generated time, if it has expired it deletes that cached file and only then accesses the controller to generate the new cache file and the cycle continues.

The same if you went in the back end and edited the content for the page. Unless the cached file has expired or doesn't exist, the old cached content will continue to be displayed until it does expire and then gets regenerated. This caused problems for a project where they needed the page to update in real time if the content changed in the backend, so we just delete any cache file that exists when something changes in the database. It's pretty easy to figure out the cached files name, because it will just be a md5() of the URI that was requested in the /application/cache dir.

Not everything should be cached. Forms for instance, like you are finding out. Luckily it's not an all or nothing option and you can fine tune what gets cached and what doesn't.

No, the URL does not change on form submission.

I have read the documentation and agree that the output class should work as you describe. However, in my case the cached file was being overwritten every time a URL is accessed, regardless if the cached file had expired or not. The only way I could prevent this from happening was to load a view without the $data parameter. To test this out I would access a specific link and then open up the cached file that was generated. I would then submit the form and then re-check the cached file. The cached file was overwritten with the most recent request (i.e. with the validation_errors() echoed), regardless if I had the cache set to expire after 60 minutes. Then I would refresh the page, check the cached file, and see that it was again overwritten - this time with no validation_errors(). Can you think of anything that would cause this to be happening?

Regardless, I switched over to the phil sturgeon library, which is working perfectly.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.