Giter Club home page Giter Club logo

kirby-form's Introduction

NOT MAINTAINED

Try https://github.com/mzur/kirby-form instead, which was based on my original plugin.


Kirby Form

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

Requires: Jevets\Kirby\Flash for storing error messages and submitted form data.

Quick Example

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

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

Installation

Require the package in your composer.json file:

{
    "require": {
        "jevets\kirby-form": "dev-master"
    },
    "repositories": [
        {
            "type": "git",
            "url": "https://github.com/jevets/kirby-form.git"
        }
    ]
}

Run composer install or composer update.

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') ?>">
        <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 ($site, $pages, $page) {

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

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

    return compact('form');

};

kirby-form's People

Contributors

jevets avatar mzur avatar

Stargazers

André Philip avatar Joro Yordanov avatar  avatar Lorenzo Ascani avatar Marco Mezzavilla avatar Till Prochaska avatar  avatar Petr Pščolka avatar

Watchers

André Philip avatar James Cloos avatar  avatar  avatar Petr Pščolka avatar

Forkers

mzur tisbert

kirby-form's Issues

Publish on Packagist

Please publish this repo on Packagist so no extra repositories URL is required in the composer.json.

Nice!

Hi Steve,

I'm the author of Kirby Uniform, another form plugin. I really like your more object oriented and well structured approach. I always wanted to do a refactoring of Uniform since it "evolved" quite a bit (if you know what I mean 😉) and, initially, I didn't really know how good PHP looks like. So I have a few questions for you:

Would you be willing to improve kirby-form to feature parity with Uniform so it can be a "better" successor? This would include CSRF and (pluggable) spam protection mechanisms as well as some analog to Uniform actions and optional multi language support. Also, it should be installable with Composer, the Kirby CLI and "standalone". If not, would you mind me borrowing some of your concepts (use Kirby\Flash, maybe extend your Form class) to develop a Uniform v3?

Edit: I couldn't wait and just started a little experiment with Uniform extending your basic Form class. I really like it. But it's nothing official yet as I'd like to hear your thoughts first.

htmlspecialchars output

kirby-form encodes all input data with htmlspecialchars (see also #6). If the data is printed with $form->old(...) it is decoded with htmlspecialchars_decode. But if the form data is directly accessed (e.g. though $form->data(...)) the data is not automatically decoded. That may be problematic.

@jevets maybe you can help out here. What was the original motivation to use htmlspecialchars? Shouldn't the form data be saved not encoded? And shouldn't htmlspecialchars be called if form data is returned with $form->old() so no special chars are printed to the HTML? I'm probably missing something here.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.