Kirby Uniform
repository·master·Indexed 42 days ago
https://github.com/mzur/kirby-uniformA versatile plugin for the Kirby CMS designed to handle web form actions, validation, and submission processing. It provides built-in actions such as emailAction, emailSelectAction, and logAction, and allows developers to create custom actions by extending the Uniform\Actions\Action base class.
What's inside Kirby Uniform
- The Honeytime Guard is a bot protection mechanism that uses a hidden form field containing an encrypted timestamp. When a form is submitted, the guard compares this timestamp against the current time. If the submission occurs faster than the configured threshold (defaulting to 10 seconds), the submission is rejected. This prevents bots from forging the field because the timestamp is encrypted using a secret key.
How Guards work in Uniform
masterGuards are the spam protection mechanism in Uniform. By default, if no guard is explicitly specified, the
HoneypotGuardis used. You can chain multiple guards together to provide layered protection. If you need to bypass all spam protection for a specific form instance, you can use thewithoutGuards()method before calling any other actions.use Uniformorm; $form = new Form; if (kirby()->request()->is('POST')) { $form->honeypotGuard() ->calcGuard(['option' => 'value']) // more guards or call actions }How the Honeypot Guard works
masterThe Honeypot Guard detects bot submissions by using a camouflaged form field. This field is visually hidden from human users but remains visible to bots. If the field contains any content upon submission, the guard rejects the request.
By default, the field is named
websiteand uses the CSS classuniform__pottyto hide itself. You must ensure theuniform__pottyclass is defined in your CSS to prevent human users from seeing the field.How guards and actions work together
masterUniform uses a layered approach to process form submissions:
- CSRF Validation: Automatically checked before any other step.
- Validation: Form fields are checked against the rules defined in the constructor.
- Guards: Mechanisms for spam protection (e.g.,
honeypotGuard,calcGuard). If no guard is specified,honeypotGuardis used by default. - Actions: The final processing step (e.g.,
emailAction,logAction).
Chaining Guards and Actions
You can chain multiple guards or actions. Note that if any step fails, the process stops and redirects back to the form.
if ($kirby->request()->is('POST')) { $form->honeypotGuard() ->calcGuard(['field' => 'result']) ->emailAction(['to' => 'user@example.com']) ->logAction(['file' => 'path/to/log.txt']); }To skip spam protection entirely, use
withoutGuards():$form->withoutGuards()->emailAction([...]);How actions work in Uniform
masterActions are used to process submitted web form data. They are only executed if the form data passes all configured guards. Multiple actions can be chained together, but if any action fails, the subsequent actions in the chain will not be executed.
If no guard is explicitly called, the first action in the chain will automatically execute the default
HoneypotGuard.To chain actions, call them sequentially on the
Uniform\ Forminstance:use Uniform\Form; $form = new Form; if (kirby()->request()->is('POST')) { $form->emailAction([/* options */]) ->logAction([/* options */]) ->webhookAction([/* options */]); }Render form fields with error handling and old values
masterWhen rendering a form in a template, use the following methods from the
$forminstance to ensure a good user experience:$form->old('field_name'): Retrieves the value previously submitted by the user so the field can be pre-populated after a validation error.$form->error('field_name'): Returnstrueif the specific field has validation errors, allowing you to apply CSS classes likeerror.$form->error('field_name')(returning array): Returns an array of error messages for that field.
For global action errors (like an
emailActionfailing), you can check for errors using the class name:$form->error(\Uniform\Actions\EmailAction::class).<input <?php if ($form->error('name')): ?> class="error"<?php endif; ?> name="name" type="text" value="<?php echo $form->old('name') ?>" > <!-- Displaying error messages --> <?php if ($form->error('name')): ?> <p class="error-text"><?php echo implode('<br>', $form->error('name')) ?></p> <?php endif; ?>Configure Email transport in Uniform v4
masterUniform v4 uses Kirby 3's native email handling via PHPMailer. You can no longer define custom email services within Uniform actions.
To configure transport settings (like SMTP), use the standard Kirby configuration. You must remove the
serviceandservice-optionskeys from theEmailActionoptions array.Update Email subject templates for Uniform v4
masterString templating in Uniform v4 (Kirby 3) uses double curly braces
{{ }}for variables instead of single braces{ }.Note on missing variables: In Uniform v4, if a variable in a template has no replacement, it will be removed from the string entirely rather than being left as literal text.
$form->emailAction([ // ... 'subject' => 'New request from {{firstname}} {{lastname}}', ]);Install Kirby Uniform via Composer
masterInstall the Kirby Uniform plugin using Composer to add it to your Kirby project.
composer require mzur/kirby-uniformConfigure Kirby cache and sessions for Uniform
masterWhen using Uniform, keep the following requirements in mind:
- Disable Kirby cache: For pages where you use Uniform, you must disable the Kirby cache to ensure forms are generated dynamically.
- Session Cookies: Uniform utilizes Kirby sessions, which requires a session cookie to be correctly set in the user's browser.
Use the Upload Action to handle file uploads
masterThe
uploadActionhandles one or more uploaded files and moves them to a specified directory.Important Security Note: The
uploadActiondoes not perform any validation (such as file size or MIME type) by itself. You must use Uniform validation rules (likefile,mime, andfilesize) to ensure uploaded files are safe and meet your requirements. Always use a whitelist of allowed MIME types and a maximum file size.$form->uploadAction(['fields' => [ 'filefield' => [ 'target' => $target_directory, 'prefix' => $prefix_string, ], ]]);Configure Kirby Uniform setup
masterAfter installation, perform the following two steps to ensure correct functionality:
- CSS Setup: Add the following rule to your CSS to hide the honeypot field used for spam protection:
.uniform__potty { position: absolute; left: -9999px; } - Disable Cache: Disable the Kirby cache for any pages where you are using Uniform. This ensures that form instances are generated dynamically and correctly handle state.
.uniform__potty { position: absolute; left: -9999px; }- CSS Setup: Add the following rule to your CSS to hide the honeypot field used for spam protection: