Hi there. I’m trying to insert two forms on one page but I keep getting error messages.
I had a single form working perfectly Using Uniform in the following way:
use Uniform\Form;
return function ($kirby)
{
$contactform = new Form([
'name' => [
'rules' => ['required'],
'message' => 'Please enter your name',
],
'email' => [
'rules' => ['required', 'email'],
'message' => 'Please enter a valid email address',
],
'company' => [],
'tel' => [],
'message' => [
'rules' => ['required'],
'message' => 'Please enter a message',
],
]);
if ($kirby->request()->is('POST')) {
$contactform->emailAction([
'to' => 'name@email.co.uk',
'from' => 'no-reply@email.co.uk',
'subject' => 'Contact form submission from website.com'
]);
}
return compact('contactform');
I then read this Answer in the Uniform docs as to how to set up multiple forms. I changed my controller to:
use Uniform\Form;
return function ($kirby)
{
$contactform = new Form([
'name' => [
'rules' => ['required'],
'message' => 'Please enter your name',
],
'email' => [
'rules' => ['required', 'email'],
'message' => 'Please enter a valid email address',
],
'company' => [],
'tel' => [],
'message' => [
'rules' => ['required'],
'message' => 'Please enter a message',
],
],'contactform');
$quickquoteform = new Form([
'qqname' => [
'rules' => ['required'],
'message' => 'Please enter your name',
],
'qqemail' => [
'rules' => ['required', 'email'],
'message' => 'Please enter a valid email address',
],
'qqcompany' => [],
'qqtel' => [],
'qqmessage' => [
'rules' => ['required'],
'message' => 'Please enter a message',
],
],'quickquoteform');
if ($kirby->request()->is('POST')) {
$contactform->emailAction([
'to' => 'name@email.co.uk',
'from' => 'no-reply@email.co.uk',
'subject' => 'Contact form submission from website.com'
]);
}
return compact('contactform');
The problem I have now is that when I try and load the page, I get an error:
Whoops \ Exception \ ErrorException (E_NOTICE)
Undefined variable: quickquoteform
This appears to be becasue my form fields follow the following structure/syntax:
<input<?php if ($quickquoteform->error('qqname')): ?> class="error"<?php endif; ?> name="qqname" id="qqname" type="text" placeholder="Your name" value="<?php echo $quickquoteform->old('qqname') ?>">
I’m struggling to understand why the variable $quickquoteform is being returned as undefined when the variable $contactform for the first form appears to work perfectly.
Because of this issue, I haven’t even got onto how I submit the correct form (the most relevant thread I can find on that goes back to Kirby 2).