I’d like to add a simple Tel: field. Where/How in this code is that done:
Controller:
<?php
return function($site, $pages, $page) {
$form = uniform('registration-form', [
'required' => [
'name' => '',
'_from' => 'email'
],
'validate' => [
'attendees' => 'num'
],
'actions' => [
[
'_action' => 'email',
'to' => 'me@example.com',
'sender' => 'info@my-domain.tld',
'subject' => 'Exhibition - New registration',
'snippet' => 'uniform-email-table'
],
[
'_action' => 'email',
'to' => 'me-too@example.com',
'sender' => 'info@my-domain.tld',
'subject' => 'Exhibition - New registration',
'snippet' => 'uniform-email-table',
'receive-copy' => false
]
]
]);
return compact('form');
};
This is my form:
<form action="<?php echo $page->url()?>#form" method="post">
<div class="row">
<div class="col-md-5">
<div class="form-group">
<input<?php e($form->hasError('name'), ' class="erroneous"')?> type="text" name="name" id="name" value="<?php $form->echoValue('name') ?>" class="form-control input-lg" placeholder="Name*"required/>
</div>
<div class="form-group">
<input<?php e($form->hasError('_from'), ' class="erroneous"')?> type="email" name="_from" id="email" class="form-control input-lg" placeholder="Email" value="<?php $form->echoValue('_from') ?>" required/>
</div>
<-- Tel: here -->
</div>
<div class="col-md-7">
<div class="form-group">
<textarea cols="6" rows="8" class="form-control input-lg" placeholder="Message" name="message" id="message"><?php $form->echoValue('message') ?></textarea>
<label class="uniform__potty" for="website">Please leave this field blank</label>
<input type="text" name="website" id="website" class="uniform__potty" />
<a name="form"></a>
<?php if ($form->hasMessage()): ?>
<div class="message <?php e($form->successful(), 'success' , 'error')?>">
<?php $form->echoMessage() ?>
</div>
<?php endif; ?>
</div>
<button class="btn btn-primary btn-lg pull-right" type="submit" name="_submit" value="<?php echo $form->token() ?>"<?php e($form->successful(), ' disabled')?>>Submit Now!</button>
</div>
</div>
</form>
After a few tries the form works but I’m uncertain how to name the fields in the form and then where to assign the field in the controller.
You only have to add it to the controller if you want to require it, otherwise you just add a label/input field to the form.
<div class="form-group">
<input<?php e($form->hasError('phone'), ' class="erroneous"')?> type="text" name="phone" id="phone" class="form-control input-lg" placeholder="Phone" value="<?php $form->echoValue('phone') ?>" />
</div>
Awesome thanks. Very little experience with contact forms. How would i then add it to the controller? Something under the ‘actions’ => section?
'_action' => 'tel
or
'tel' => 'tel'
I don’t really get how this works
Never mind. It’s early where I am. Not fully awake. All good now. I don’t need to require it. All works fine. Thank you very much !
I should have been more explicit. If you need new fields, you simply add them to the form with a unique name. If you want to require a field, it goes into the required
array in the controller:
'required' => [
'name' => '',
'_from' => 'email'
'phone' => '',
],
If it is not required, you don’t do anything else, the field is processed automatically.
1 Like
emil
April 13, 2017, 7:49am
6
Hi all!
For a current project i need something similar, but i’m using Uniform V3. I have searched a while now for something similar for the latest version, but am not able to find it.
The project needs a form with the ability to add an array (foreach loop) of input(checkbox) tags with names and values etc. added from the panel using a structured field.
Then get the form to send those that are checked when the form is validated. The checkboxes will not be requiered fields, only options.
I would be eternally grateful if you could help!
What exactly is your problem? Generating the checkboxes? Validating the input?
emil
April 13, 2017, 9:31am
8
The problem is validating the input. At the moment unless the input fields are not represented in the controller by nameing them like 'name' => [ ],
they are not processed/validated.
I have just take the basic example from Uniform to illustrate what I mean.
The controller:
<?php
use Uniform\Form;
return function ($site, $pages, $page)
{
$form = new Form([
'email' => [
'rules' => ['required', 'email'],
'message' => 'Please enter a valid email address',
],
'name' => [],
'message' => [
'rules' => ['required'],
'message' => 'Please enter a message',
],
]);
if (r::is('POST')) {
$form->emailAction([
'to' => 'me@example.com',
'from' => 'info@example.com',
]);
}
return compact('form');
};
**The Template:**
<?php snippet('header') ?>
<h1><?php echo $page->title()->html() ?></h1>
<style type="text/css">
label, input, textarea {
display: block;
}
.uniform__potty {
position: absolute;
left: -9999px;
}
.error {
border: 1px solid red;
}
</style>
<form action="<?php echo $page->url() ?>" method="POST">
<label>Email</label>
<input<?php if ($form->error('email')): ?> class="error"<?php endif; ?> name="email" type="email" value="<?php echo $form->old('email') ?>">
<label>Name</label>
<input<?php if ($form->error('name')): ?> class="error"<?php endif; ?> name="name" type="text" value="<?php echo $form->old('name') ?>">
<?php foreach($page->object()->yaml() as $object): ?>
<input
type="checkbox" name="<?php echo $object['name'] ?>"
value="<?php echo $object['value'] ?> "> <?php echo $object['label'] ?>
<br>
<?php endforeach ?>
<label>Message</label>
<textarea<?php if ($form->error('message')): ?> class="error"<?php endif; ?> name="message"><?php echo $form->old('message') ?></textarea>
<?php echo csrf_field() ?>
<?php echo honeypot_field() ?>
<input type="submit" value="Submit">
</form>
<?php if ($form->success()): ?>
Thank you for your message. We will get back to you soon!
<?php else: ?>
<?php snippet('uniform/errors', ['form' => $form]) ?>
<?php endif; ?>
<?php snippet('footer') ?>
thank you:)
You checkbox input fields should all have the same name with square brackets (indication an array of values) , otherwise you can’t reference the field in your form controller:
name="whatever[]"
emil
April 13, 2017, 10:05am
10
Ok, so it i give it the name of name="objects[]"
<?php foreach($page->objects()->yaml() as $object): ?>
<input
type="checkbox" name="objects[]"
value="<?php echo $object['value'] ?> "> <?php echo $object['label'] ?>
<br>
<?php endforeach ?>
Then in the controller how would i reference that name? Sorry, for the inconvenience
Should be:
$form = new Form([
'email' => [
'rules' => ['required', 'email'],
'message' => 'Please enter a valid email address',
],
'name' => [],
'objects' => [],
'message' => [
'rules' => ['required'],
'message' => 'Please enter a message',
],
]);
For validation, I’m afraid you have to implement your own validator , as there is none in Kirby that accepts an array as value.
emil
April 13, 2017, 10:23am
12
Thank you! I will give it a go:)