Forms with Uniform, one parent template and several form snippets

How do I setup my controller, if I want to use one parent template, that loads snippets with different forms. Here is what I have:

site/templates/form.blade.php


@extends('layouts.default')

@section('content')
        // could be a kirby snippet
        // <?= snippet($page->uid()) ?>
	@includeIf('forms.' . $page->uid())
@endsection

/site/controllers/form.php - as far as I understand, this is the parent controller and I need so extend it somehow, but how?

<?php
use Uniform\Form;
return function ($kirby, $pages, $page)
{
   // should the following go into a file that I include with
   // require sprintf('/%s.php', $page->uid())

    $form = new Form([
    ]);

    if ($kirby->request()->is('POST')) {
    }

    //ddd($form);
    return compact('form');
};

/site/templates/forms/besucheranmeldung.blade.php

<form method="POST">
	<input name="email" type="email" value="<?php echo $form->old('email') ?>">
	<textarea name="message"><?php echo $form->old('message') ?></textarea>
	<select name="recipient">
		<?php $value = $form->old('recipient') ?>
		<option value="sales"<?php e($value=='sales', ' selected')?>>Contact sales</option>
		<option value="marketing"<?php e($value=='marketing', ' selected')?>>Contact marketing</option>
		<option value="feedback"<?php e($value=='feedback', ' selected')?>>Give feedback</option>
	</select>
	<?php echo csrf_field() ?>
	<?php echo honeypot_field() ?>
	<input type="submit" value="Submit">
</form>

/content/5_kontakt/besucheranmeldung/form.de.txt

Title: Besucheranmeldung
----
Slug: besucheranmeldung

I have to create a lot of forms and don’t want to create too many templates and blueprints, or am I overlooking something?

The controller (controllers/form.php) is determined by the content/**/besucheranmeldung/form.txt, but is there a place to define that /controllers/besucheranmeldung.php should be loaded?

How to approach this the best?

Which page uses this controller? And why is there a subfolder inside templates? Is that part of the blade plugin, because Kirby doesn’t allow subfolders in /templates? Would be helpful to understand the structure a bit more.

Right now none, I wish I could define that it’s used by content/**/besucheranmeldung/form.txt

Does that make sense?

This should be loaded instead of /controllers/form.php

But why?

To have a controller for every page with the form template instead one controller only for the form template.

You can use a route that does the form handling instead of an additional controller. Or you can use a general controller that you require inside the other controllers. But then you would have to require it all the time. Therefore, a route makes more sense from my perspective.

That can be read as /site/snippets/besucheranmeldung.php - sorry, that wasn’t clear.

The general controller would be something like that:

<?php
use Uniform\Form;
return function ($kirby, $pages, $page)
{
   require besucheranmeldung.php
   // or more dynamic:
   // require sprintf('/%s.php', $page->uid())

    return compact('form');
};

right? where besucheranmeldung.php contains this

$form = new Form([
    ]);

    if ($kirby->request()->is('POST')) {
    }

What would the route look like if I go that way?

Thanks!

You can find more information about merging a shared controller here: https://getkirby.com/docs/cookbook/templating/shared-controllers

You can find more information about routes here: https://getkirby.com/docs/guide/routing

It works basically the same as calling an external script.

1 Like

Awesome! I was hoping there is something like $kirby->controller() but I got lost in the docs and wasn’t sure what to look for!
Thank you so much. I will post the result, once I put everything together successfully :slight_smile: