Kirby Uniform form not sending

For some reason i cant seem to get Unifrom (v4) to send anything. The validation rules work (i get the error messages on the page), but if i fill the forms out properly and hit submit, it just reloads the page, doesn’t send anything, and does not attempt to redirect to the thank you page. I had this happen before once and solved it but what worked then does not work now.

Here is my controller… there are two forms setup in it. One of the forms is on every single page. The other form is only on one page, along with the other form.

<?php

use Uniform\Form;

return function($kirby, $site, $pages, $page) {

// Forms

// TRADE FORM
$form = new Form([
'name' => [
  'rules' => ['required'],
  'message' => 'Enter your name',
],

'email' => [
    'rules' => ['required', 'email'],
    'message' => 'Enter a valid email',
],
'company' => [
  'rules' => ['required'],
  'message' => 'Enter company name',
],
'btype' => [
    'rules' => ['required'],
    'message' => 'Enter business type',
],

'message' => [
    'rules' => ['required'],
    'message' => 'Enter a message',
],


], 'contact-form');

// FOOTER FORM
$newsform = new Form([
'fname' => [
'rules' => ['required'],
'message' => 'Enter your name',
],

'femail' => [
  'rules' => ['required', 'email'],
  'message' => 'Enter a valid email',
]

], 'footer-form');


if (r::is('POST')) {
    if (get('form') === 'contact') {
      $form->emailAction([
        'to' => 'hello@domain.com',
        'from' => 'noreply@domain.com',
        'subject' => 'Contact Form'
      ]);
        if ($form->success()) {
        // redirect to success page
        go('/thanks');
      }
    } elseif (get('form') === 'news') {
      $newsform->emailAction([
        'to' => 'hello@domain.com',
        'from' => 'noreply@domain.com',
        'subject' => 'News Signup'
      ]);

      if ($newsform->success()) {
      // redirect to success page
      go('/thanks');
    }

    }
}

return compact('form', 'newsform');

};

it seems you got multi form setup right. hm…

you could try dumping the plain server post vars to get a response.

if (r::is('POST')) {
   var_dump($_POST); die;
   //...
}

Thanks @bnomei that gives me this…

array(4) { ["fname"]=> string(5) "James" ["femail"]=> string(27) "email@domains.co.uk" ["form"]=> string(4) "news" ["csrf_token"]=> string(64) "1a673ea075295b00f6b062e22ed70f92174771cb591518713afb847741fa7011" }

Which is posting ok then i guess… hrmmm

try using the build in logging action instead of the email action to check if your email setup for the newsletter is causing trouble. the log action should not fail (like the email could).

Ok I tried that, it is not writing the log file to the server, for either of the forms. Weird. I tried locally and on a real web server.

  1. try var_dumping the $newsform object.
  2. comment /* */ the $form object to check if uniform messes up if there are two forms.

Setting up just one form on the page does not work, same result as before.

The var dump works, for both forms. I’m just at a loss why its not actually sending it.

:man_shrugging:

i would continue in removing everything concerning the two form. redo the the newsform with different vars and form id names until i got that working. then add main form step by step.

restart mac and browser, local dev system etc.

Ive tried every thing. Even switched PHP versions on the live server. Ive run out of ideas. Oddly, i’m using the same code i use on another which does work (although that one isnt multiform).

@mzur I dont suppose you have any suggestions?

Could you post all relevant files here for testing? Or zip them up?
Also, what Kirby version are you using?

I was using 3.1.3 until a few minutes ago when 3.1.4 went up.

Controller…

<?php

use Uniform\Form;

return function($kirby, $site, $pages, $page) {

// Import Global Controller
require_once kirby()->root('controllers') . '/shared/global.php';

// Forms

// TRADE FORM
$form = new Form([
'name' => [
  'rules' => ['required'],
  'message' => 'Enter your name',
],

'email' => [
    'rules' => ['required', 'email'],
    'message' => 'Enter a valid email',
],
'company' => [
  'rules' => ['required'],
  'message' => 'Enter company name',
],
'btype' => [
    'rules' => ['required'],
    'message' => 'Enter business type',
],

'message' => [
    'rules' => ['required'],
    'message' => 'Enter a message',
],


], 'contact-form');

// FOOTER FORM
$newsform = new Form([
'fname' => [
'rules' => ['required'],
'message' => 'Enter your name',
],

'femail' => [
  'rules' => ['required', 'email'],
  'message' => 'Enter a valid email',
]

], 'footer-form');

if (r::is('POST')) {
    if (get('form') === 'contact') {

        $form->logAction([
            'file' => kirby()->roots()->site().'/contactmessages.log',
        ]);


        if ($form->success()) {
        // redirect to success page
        go('/thanks');
      }
    } elseif (get('form') === 'news') {
      
        $newsform->logAction([
            'file' => kirby()->roots()->site().'/newsmessages.log',
        ]);

      if ($newsform->success()) {
      // redirect to success page
      go('/thanks');
    }

    }
}


return compact('form', 'newsform');
};

news form…

<!-- form -->
<form class="form" id="news" action="<?= $page->url() ?>/#news" method="post">
<div class="footer-form">

  <div class="block-2">
  <div class="block-col">
    <ul class="form-flex">
      <li><?= snippet('forms/inputs/fields', ['label' => 'Name:', 'name' => 'fname', 'type' => 'text', 'formname' => $newsform]) ?></li>
    </ul>
  </div>

  <div class="block-col">
    <ul class="form-flex">
      <li><?= snippet('forms/inputs/fields', ['label' => 'Email:', 'name' => 'femail', 'type' => 'text', 'formname' => $newsform]) ?></li>
    </ul>
  </div>
  </div>

  <div class="block-1">
  <div class="block-col">
    <input type="hidden" name="form" value="news">
    <?= csrf_field(); ?>
    <button class="btn" type="submit" name="_submit" value="<?= $form->token() ?>"<?php e($newsform->successful(), ' disabled') ?>>Submit</button>
  </div>
  </div>

</div>
</form>
<!-- form -->

Contact form…

<form class="form" id="contact" action="<?= $page->url() ?>/#contact" method="post">

<div class="block-2 <?= $state ?>">

  <div class="block-col">
  <ul class="form-flex">
  <li><?= snippet('forms/inputs/fields', ['label' => 'Name:', 'name' => 'name', 'type' => 'text', 'formname' => $form]) ?></li>
  <li><?= snippet('forms/inputs/fields', ['label' => 'Company:', 'name' => 'company', 'type' => 'text', 'formname' => $form]) ?></li>
  </ul>
  </div>

  <div class="block-col">
  <ul class="form-flex">
  <li><?= snippet('forms/inputs/fields', ['label' => 'Email:', 'name' => 'email', 'type' => 'text', 'formname' => $form]) ?></li>
  <li><?= snippet('forms/inputs/fields', ['label' => 'Business Type:', 'name' => 'btype', 'type' => 'text', 'formname' => $form]) ?></li>
  </ul>
  </div>

</div>

<div class="block-1 <?= $state ?>">

  <div class="block-col">
  <ul class="form-flex">
  <li><?= snippet('forms/inputs/fields', ['label' => 'Message:', 'name' => 'message', 'type' => 'textarea', 'formname' => $form]) ?></li>
  </ul>
  </div>
</div>

<div class="block-1 <?= $state ?>">
  <div class="block-col">
    <input type="hidden" name="form" value="contact">
    <?= csrf_field(); ?>
    <button class="btn" type="submit" name="_submit" value="<?= $form->token() ?>"<?php e($form->successful(), ' disabled') ?>>Submit</button>
  </div>
</div>

</form>

Heres the magic snippet i used to generate the inputs…

<?php $fname = $formname ?? $form;?>

<?php if($type == 'textarea'): ?>

<div class="message">
<label for="<?= $name ?>"><?= $label ?></label>
<textarea <?php if ($fname->error($name)): ?> class="invalid"<?php endif; ?> name="<?= $name ?>" id="<?= $name ?>"><?php $fname->old($name) ?></textarea>
</div>

<?php if ($fname->error($name)): ?>
  <p class="form-error-hint"><?= implode('<br>', $fname->error($name)) ?></p>
<?php endif; ?>

<?php elseif($type == 'text' || $type == 'email' || $type == 'tel' || $type == 'num'): ?>

<div>
<label for="<?= $name ?>"><?= $label ?></label>
<input<?php if ($fname->error($name)): ?> class="invalid"<?php endif; ?> name="<?= $name ?>" id="<?= $name ?>" type="<?= $type ?>" value="<?= $fname->old($name) ?>">
</div>

<?php if ($fname->error($name)): ?>
  <p class="form-error-hint"><?= implode('<br>', $fname->error($name)) ?></p>
<?php endif; ?>

<?php endif ?>

Ive just noticed that for some reason Uniform thinks there has an error with the form. Despite filling in all the fields, its $form->error() is getting triggered somehow - im getting “incomplete” classes added every where (i told it to do this). However i have no idea what the error actually is.

Edit:

Add the error snippet… it tells me this… what the heck??

The form field that is supposed to be empty was filled. In case you are not a spam-bot, please try again leaving the field blank.

Turns out it was a missing honeypot. Oddly though, that was in place earlier when it didnt. Weird.

It works now, and is writting the log to the server.

1 Like