Using Uniform contact form in snippet not in template (2)

Hi!

I’m trying to use Uniform to create a contact form in a snippet that I could use in certain templates. I also would like to have a page where I could modify the content of this snippet. So far, what I have:

  • /snippets/snippet-quote-request-form.php: Snippet with the form
  • /templates/snippet-quote-request-form.php: Template that makes a preview of the snippet, for use in the Panel
  • /blueprints/snippet-quote-request-form.yml: For use with the above mentioned template
  • /templates/template-has-header-cta.php: Template that loads /snippets/snippet-quote-request-form.php

In /templates/template-has-header-cta.php here’s the relevant line:

<?php snippet('snippet-quote-request-form') ?>

In /snippets/snippet-quote-request-form.php, I have:

<?php
use Uniform\Form;

return function ($site, $pages, $page)
{
  $form = new Form([
    'email' => [
        'rules' => ['required', 'email'],
        'message' => 'Please enter a valid email address.',
    ],
    'name' => [],
    'phone' => [],
  ]);

  if (r::is('POST')) {
    $form->emailAction([
        'to' => 'rodrigo@x.com',
        'from' => 'rodrigo@x.com',
    ]);
  }

  return compact('form');
};
?>

<!-- Sets template URL for this snippet -->
<!-- From this page I'm taking 'form_title', 'form-lead', 'form_field_name', etc. -->
<?php $p = page('snippet-quote-request-form') ?>

<form class="form form-quote-request" action="<?= $p->url() ?>" method="POST">
  <h2><?= $p->form_title()->html() ?></h2>
  <?= $p->form_lead()->kirbytext() ?>

  <label for="name"><?= $p->form_field_name()->html() ?></label>
  <input type="text" id="name" placeholder="<?= $p->form_field_name()->html() ?>" value="<?php echo $form->old('name'); ?>" />

  <label for="email"><?= $p->form_field_email()->html() ?></label>
  <input type="text" id="email" placeholder="<?= $p->form_field_email()->html() ?>" value="<?php echo $form->old('email'); ?>" />

  <label for="phone"><?= $p->form_field_phone()->html() ?></label>
  <input type="text" id="phone" placeholder="<?= $p->form_field_phone()->html() ?>" value="<?php echo $form->old('phone'); ?>" />
  
  <?= csrf_field(); ?>
  <?= honeypot_field(); ?>

  <input type="submit" value="<?= $p->form_button()->html() ?>" />
</form>

<?php if ($form->success()): ?>
  <p>Success!</p>
<?php else: ?>
  <?php snippet('uniform/errors', ['form' => $form]); ?>
<?php endif; ?>

So, the snippet doesn’t load in a test page that uses the template /templates/template-has-header-cta.php. I tried removing the controller code from the snippet, and I got an error: Undefined variable: form. Therefore, the snippet is loading fine, but it seems I’m messing up with the controller code.

Any ideas?

Remove the controller callback, only this should go into the snippet:

use Uniform\Form;

  $form = new Form([
    'email' => [
        'rules' => ['required', 'email'],
        'message' => 'Please enter a valid email address.',
    ],
    'name' => [],
    'phone' => [],
  ]);

  if (r::is('POST')) {
    $form->emailAction([
        'to' => 'rodrigo@x.com',
        'from' => 'rodrigo@x.com',
    ]);
  }

Great, that worked. Now, I’m not receiving the email. I’m trying this directly on the server. Any clues on where to look at?

Naturally, I changed 'to' => 'rodrigo@x.com' to the proper email address.

Have you already tried the Dump or Log Action to check if all goes well apart from sending the mail?

As regards PHP sendmail, it must be enabled on your server. Have you tried sending mail from that server before?

Have you already tried the Dump or Log Action to check if all goes well apart from sending the mail?

No, how do I do this?

As regards PHP sendmail, it must be enabled on your server. Have you tried sending mail from that server before?

Never. It’s a Digital Ocean Droplet with LAMP 16.04 installed. It seems I might need to install Postfix and do other tricks :scream_cat:

The logAction is a built-in action that you can use like this (in addition to the email action or instead of it)

    if (r::is('POST')) {
        $form->logAction([
            'file' => kirby()->roots()->site().'/messages.log',
        ]);
    }

As regards sendmail, try to send a mail from the command line, see here: https://clients.javapipe.com/knowledgebase/132/How-to-Test-Sendmail-From-Command-Line-on-Linux.html

And enable on Digital Ocean check out this: https://www.digitalocean.com/community/questions/php-mail-function-enable, or try to find some newer docs.

However, it is generally a good idea to use either a mail service or PHPMailer instead of sendmail for sending mail.

1 Like

Thanks! I will check these resources now.