mzur / kirby-form
基于 Kirby 的网站表单助手,使用 Post/Redirect/Get 模式。
v3.3.0
2023-02-19 09:51 UTC
Requires
- getkirby/composer-installer: ^1.2
- mzur/kirby-flash: ^2.0
Requires (Dev)
- getkirby/cms: ^3.0
- mzur/kirby-defuse-session: ^1.0
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2024-09-19 14:16:50 UTC
README
这是 jevets\kirby-form 的分支。
用于处理 Kirby 表单的助手库,使用 Post/Redirect/Get 设计模式。
快速示例
$form = new Form([ 'name' => [ 'rules' => ['required'], 'message' => ['Name is required'] ], 'phone' => [], ]); if ($form->validates()) { // Validation passed // Do something with the data }
安装
使用 composer 安装
# Kirby 2 composer require mzur/kirby-form:^1.0 # Kirby 3 composer require mzur/kirby-form:^2.0
基本示例
此示例假设您正在使用 Kirby 的页面控制器,并且您的页面 URI 为 /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'); };
贡献
欢迎发送拉取请求!
问题/错误
请使用 GitHub 问题跟踪器。