Uniform with virtual pages

So, I already asked this on the Uniform repo but was asked to come to the forum to check if someone has an answer.

I tried to create a virtual page which renders a template with a form (using the Uniform plugin) in it. Turns out the $form instance isn’t available in the template when it is rendered that way.

Testing by rendering the template directly, not as a virtual page, it works flawlessly.

Anyone has experience with using Uniform with virtual pages? Is there a trick to make the $form instance available in the templates?

How is your virtual page registered and why is it a virtual page in the first place?

The short answer:

Because with a virtual page it enables those forms to exist in every environment I deploy to without creating a content file for them.

The long answer:

The reason why I consider using a virtual page is the fact, that I use Kirby headless. The only thing rendered in the front-end of Kirby are forms, which are integrated into the front-end either by using an iframe or by linking to the form on the CMS front-end and progressively enhancing this by replacing the link with JavaScript and directly load the form into the front-end via an async request (in my case I use Turboframes).

Forms are de facto the only thing I can’t do in a static generated website, progressive enhanced to work without JavaScript, without using the server-side rendered output of Kirby here.

Now, when deploying to different environment in the CI system, that currently requires me to create a content file, on every environment, to make the form render. With a virtual page, I could ensure those forms are just there, in every environment, that they don’t depend on a content file to exist.

For the virtual page setup itself: Nothing complicated. Really just what I got from the simple virtual page example in the documentation, with additional GET|OPTIONS configuration.

All this stuff works flawlessly when I manually handle my forms. When trying to do it with Uniform, it seems like the plugin is not available in the virtual page. This could either be a bug or a missing feature of the plugin, or there is something I’m missing I need to do in the virtual page setup to pass the instance.

Probably something like this (which was given as answer after I created this post here):

Did the suggestion work for you?

I eventually settled with not using Uniform and built myself some shared controllers and snippets for a similar experience, as I found I still have this second problem with not being able to deactivate the CSRF check in Uniform (noted in a different issue). So, I’m not certain if I’m going to test the proposed, guessed solution for a while and closing this. For now.

Thanks, anyway for everyone who already invested time to read through my post and suggest solutions.