mzur/kirby-form

A form helper for Kirby-based websites, using the Post/Redirect/Get pattern.

Maintainers

👁 mzur

Package info

github.com/mzur/kirby-form

Type:kirby-plugin

pkg:composer/mzur/kirby-form

Statistics

Installs: 74 838

Dependents: 1

Suggesters: 0

Stars: 8

Open Issues: 3

v3.3.0 2023-02-19 09:51 UTC

Requires (Dev)

Suggests

None

Provides

None

Conflicts

None

Replaces

None

MIT 8dc211b321375dbf77c43e1287e9fe0be2762ec0

  • Steve Jamesson <steve.woop@evendev.com>
  • Martin Zurowietz <martin.woop@the-inspired-ones.de>

formkirby


README

👁 Tests

This is a fork of jevets\kirby-form.

A helper library for working with Kirby forms, using the Post/Redirect/Get design pattern.

Quick Example

$form = new Form([
 'name' => [
 'rules' => ['required'],
 'message' => ['Name is required']
 ],
 'phone' => [],
]);

if ($form->validates()) {
 // Validation passed
 // Do something with the data
}

Installation

Install with composer:

# Kirby 2
composer require mzur/kirby-form:^1.0
# Kirby 3
composer require mzur/kirby-form:^2.0

Basic Example

This example assumes you're using page controllers in Kirby and that your page's URI is /my-page.

// site/templates/my-page.php

<?php snippet('header') ?>

 <?php snippet('form-errors', ['form' => $form]) ?>

 <form method="POST">
 <input name="name" value="<?= $form->old('name') ?>">
 <input name="phone" value="<?= $form->old('phone') ?>">
 <?= csrf_field() ?>
 <input type="submit" value="Submit">
 </form>

<?php snippet('footer') ?>
// site/snippets/form-errors.php

<?php if (count($form->errors()) > 0): ?>
 <div class="alert alert-error">
 <?php foreach ($form->errors() as $key => $errors): ?>
 <div><?= implode('<br>', $errors) ?></div>
 <?php endforeach ?>
 </div>
<?php endif ?>
// site/controllers/my-page.php

use Jevets\Kirby\Form;

return function ($kirby) {

 // Initialize the Form
 $form = new Form([
 'name' => [
 'rules' => ['required'],
 'message' => ['Name is required']
 ],
 'phone' => [],
 ]);

 // Process the form on POST requests
 if ($kirby->request()->is('POST')) {
 if ($form->validates()) {
 // Show a thanks page
 } else {
 // Redirect back to the GET form
 go('/my-page');
 }
 }

 return compact('form');
};

Contributing

Feel free to send a pull request!

Issues/Bugs

Please use the GitHub issue tracker.