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?