Renaming Uniform fields for more readable confirmation emails

Hi all - using Uniform and trying to find if there’s a way to label a field to something other than the id for more readable confirmation emails. So for example, the controller below results in a confirmation email sent where ‘firstName’ is written in the body. I would love to be able to define a readable label for it in the controller but can’t find anything in the docs. Of course I can’t use ‘First Name’ in the ID due to the spaces, but maybe I’m missing something. Thanks

<?php

use Uniform\Form;

return function ($kirby)
{
    $form = new Form([
        'firstName' => [],
        'email' => [
            'rules' => ['required', 'email'],
            'message' => 'Please enter a valid email address',
        ],
        'message' => [
            'rules' => ['required'],
            'message' => 'Please enter a message',
        ],
        
    ]);

    if ($kirby->request()->is('POST')) {
        $form->emailAction([
            'to' => 'name@email.com',
            'from' => 'name@email.com',
        ]);
    }

    return compact('form');
};

You can overwrite the plugins default email templates: kirby-uniform/templates/emails at master · mzur/kirby-uniform · GitHub by putting templates with the same name into site/emails, or define a custom snippet in your controller, see the plugin’s documentation.

Thanks - struggling a bit with this though. I cannot find anywhere in the documentation that’s mentioned and can’t see an example of renaming a field in the email templates. My paygrade may not bridge the gap on this unfortunately and was hoping it might be a little more simple.

If you look at the default template here: https://github.com/mzur/kirby-uniform/blob/master/templates/emails/default.php, you see this line #14:

    echo ucfirst($field).': '.$value."\n";

So it takes the fieldname, capitalizes the first letter and then the value.

I

The fieldname comes from your form, so you could either rename those fields in the form, or you create an array in your config, where you could then map those fields to something more useful:

// rest of config settings
'formFields' => [
  'firstName' => 'First name',
  'lastName' => 'Last name',
],

Then in your custom email template, you can fetch the values from this array

echo (option('formFields')[$field] ?? ucfirst($field)) . ': ' .$value . "\n";

So if the key exists in our config array, we get the value, if not, we fall back to the field name as before.

Thanks so much for the reply.

When you say it takes the field name, do you mean ID? It would be great if I could just said the input name in the markup to name="first name" but this doesn’t work, returning nothing in the email. The constructor in the config seems based on the ID, which can’t have whitespace.

So I then have tried your suggestion, but no luck unfortunately.

Start of plugins/uniform/config.php:

<?php

use Kirby\Cms\App as Kirby;

Kirby::plugin('mzur/kirby-uniform', [
    'templates' => [
        'uniform/log-json' => __DIR__.'/templates/log-json.php',
        'emails/uniform-default' => __DIR__.'/templates/emails/default.php',
        'emails/uniform-table' => __DIR__.'/templates/emails/table.php',
    ],
    'snippets' => [
        'uniform/errors' => __DIR__.'/snippets/errors.php',
    ],
    'formFields' => [
        'firstName' => 'First name',
        'lastName' => 'Last name',
    ],

And plugins/uniform/templates/email/default.php:

<?php

foreach ($data as $field => $value) {
    if (in_array($field, ['_data', '_options'])) {
        continue;
    }

    if (is_array($value)) {
        $value = implode(', ', array_filter($value, function ($i) {
            return $i !== '';
        }));
    }

    echo (option('formFields')[$field] ?? ucfirst($field)) . ': ' .$value . "\n";
}

Just trying to get that first name field working at the moment, but I just get the label back in an email with no content.

Thanks so much!

This array must go in your config.php, don’t touch the plugin. And don’t edit the default snippet, but put a copy into your /site/templates/emails folder.

Thanks - still no luck unfortunately.

Do I need to change constructor in my controller? It’s currently like this:

<?php

use Uniform\Form;

return function ($kirby)
{
    $form = new Form([
        'FirstName' => [],
        'email' => [
            'rules' => ['required', 'email'],
            'message' => 'Please enter a valid email address',
        ],
        'message' => [
            'rules' => ['required'],
            'message' => 'Please enter a message',
        ],
    ]);

    if ($kirby->request()->is('POST')) {
        $form->emailAction([
            'to' => 'email@domain.com',
            'from' => 'email@domain.com',
        ]);
        $form->logAction([
            'file' => kirby()->roots()->site().'/messages.log',
        ]);
    }

    return compact('form');
};

My site config is now:

<?php

return [
    'debug' => true,
    'thumbs' => [
        'srcsets' => [
            'default' => [320, 1200],
            'breakpoints' => [576, 768, 992, 1200],
        ],
    ],
    'formFields' => [
        'firstName' => 'First name',
        'lastName' => 'Last name',
    ]
];

And email was moved to a new ‘emails/default.php’ within the site/templates folder.

Markup for field in question is:

<input id="firstName" type="text" name="firstName" class="form-control" value="<?php echo $form->old('firstName'); ?>" placeholder="First Name" required data-error="Last name is required.">

vs

different spelling