Ajax and Uniform

Hello there,
I am pretty new to Kirby and also to Php. :slight_smile: Great to start, I know. Luckily Kirby lets me start very soft and smooth. Now I come to the point where I need a form on my page. And I don’t want the whole page to re-render when submitting the form and stuff. So I thought I have to use the AJAX example. And, it really works fine. But there are several things that are not nice in my opinion, so here’s my setup:

I got a template ‘contact.php’ and a controller ‘contact.php’ working… that’s cool so far.
No I got this router-settings from the Tutorial in the docs and wrote it in my site/config/config.php
But now I have the form initialization twice - first in my controller and second in my config.

That does not seem to be correct. Do I have to put the whole “router stuff” inside of my contact-controller? Or Otherwise?

Thanks a lot for your help.

Kind Regards

Which tutorial are you referring to?

I am talking about this tutorial:


It says it uses a Route as the endpoint for form submission. So I thought I have to put it in the config.
When I remove the ‘controller’ file it messes up everything.

Could you please post your controller?

Sure, here it is:

   use Uniform\Form;

    return function ($kirby)
        $form = new Form([
           'name' => [
             'rules' => [],
             'message' => 'Bitte geben Sie einen Namen ein.'
           'company' => [],
           'email' => [
             'rules' => [],
             'message' => 'Bitte geben Sie eine gültige E-Mail Adresse ein.'
           'subject' => [
             'rules' => ['required', 'in' =>
                    'Allgemeine Anfrage',
                    'Frage zu einem Projekt',
             'message' => 'Bitte wählen Sie einen Betreff aus.'
          'message' => [
            'rules' => [],
            'message' => 'Bitte geben Sie eine Nachricht ein.',
          'dataPolicy' => [
            'rules' => [],
            'message' => 'Stimmen Sie den Datenschutzrichtlinien zu.'

       if ($kirby->request()->is('POST')) {
           'to' => 'xxx@xxx.de',
           'from' => 'xxx@xxx.de',
           'subject' => 'Kontaktformular-Anfrage - {{subject}}'
       //@todo: remove before go live
         'file' => kirby()->roots()->site().'/form-messages.log'
  return compact('form');

Sorry, I tried to indent it as good as possible from hand! :smiley:

And what error do you get if you remove the controller?

When I remove the complete code from the controller I get
Undefined variable: form
from my template.

I guess I found the problem. In the template I echo the input value via

<?= $form->old('email') ?>

But in the Ajax-Tutorial it seems like yout get the values on other ways?

Yes, the Ajax example doesn’t use these values. Since the page is not reloaded the field won’t be emptied.

Thanks for that quick replies.
Removing the

<?= $form->old('xxx') ?>

fixed the problem. All I need is initialization in the router now. <3