Skip to content

Formr

Formr is Discontinued

Formr is no longer maintained. Use Flick instead - the modern successor from the same developer.

Migrate automatically: composer require --dev flickphp/migrate

Formr is a simple, yet powerful PHP form builder which enables you to create and validate your forms in seconds.

Latest Version on Packagist Total Downloads GitHub stars GitHub forks GitHub issues GitHub license

Features

  • Create complex forms with server-side processing and validation in seconds
  • Built-in support for Bootstrap and Bulma
  • Built-in support for reCAPTCHA v3
  • Built-in POST validation rules, including validating email, regex, comparisons, slugging, and hashing
  • Instantly make one field required, all fields required, or all but one field required
  • Create and validate radio groups and checkbox arrays in seconds
  • Upload images: resize, rename, and create thumbnails
  • Extensible: easily create and save your own field element wrappers
  • Extensible: easily create and save your own dropdown menus
  • Extensible: easily create and save your own form & validation sets
  • Send plain text and HTML emails
  • Generate CSRF tokens and honeypots
  • Object-oriented; supports multiple forms per page
  • Little helpers to assist in building, layout, testing and debugging
  • And a ton of other cool stuff!

Installation

Composer

Run the following command to install Formr with Composer.

composer require formr/formr

Then include the autoload.php file and create a new form object.

require_once 'vendor/autoload.php';
$form = new Formr\Formr();

Download

Download the .zip file and place the Formr folder in your project, then include the Formr class and create a new form object.

require_once 'Formr/class.formr.php';
$form = new Formr\Formr();

Frameworks

Formr has support for the Bootstrap and Bulma frameworks right out of the box. Just tell Formr which one you want to use when creating a new form and Formr will take care of the rest!

$form = new Formr\Formr('bulma');
$form = new Formr\Formr('bootstrap');
$form = new Formr\Formr('bootstrap4');

Quick Start

1. Create the Form

To use Formr you will need to create a new form object. You can name it anything you like, so let's keep it simple and call it $form.

$form = new Formr\Formr();

There are two arguments we can pass to our new form: the Wrapper, and the Switch. The Wrapper tells Formr how you want your forms to be laid out; such as with Bootstrap, Bulma, or just a simple <div> tag.

$form = new Formr\Formr('bootstrap');

Formr will automatically echo form elements and messages to the screen, and this is usually fine. However, in some instances this is not an option, such as when using a templating framework. In these cases simply pass hush to the Switch parameter and then manually echo your elements and messages.

$form = new Formr\Formr('bootstrap', 'hush');

You can also omit a framework's wrapping div (e.g.; <div class="form-group">) by adding nowrap to the switch parameter.

$form = new Formr\Formr('bootstrap', 'nowrap');

You can use multiple switches by separating each with a comma.

$form = new Formr\Formr('bootstrap', 'hush, nowrap');

2. Build the Form

To build a form, simply enter your form labels as a comma separated string and Formr will build it, complete with opening and closing tags, a submit button, and email validation; plus all form values are retained upon $_POST.

Note

All field elements default to <input type="text">, however you can easily specify another type by adding a pipe character and then the element type.

Example

require_once 'vendor/autoload.php';
$form = new Formr\Formr('bootstrap');

$form->create_form('Name, Email, Comments|textarea');

Produces the following HTML

<form action="/index.php" method="post" accept-charset="utf-8">
    <div id="_name" class="form-group">
        <label class="control-label" for="name">
            Name
        </label>
        <input type="text" name="name" id="name" class="form-control">
    </div>
    <div id="_email" class="form-group">
        <label class="control-label" for="email">
            Email
        </label>
        <input type="email" name="email" id="email" class="form-control">
    </div>
    <div id="_comments" class="form-group">
        <label class="control-label" for="comments">
            Comments
        </label>
        <textarea name="comments" id="comments" class="form-control"></textarea>
    </div>
    <div id="_button" class="form-group">
        <label class="sr-only" for="button"></label>
        <button type="submit" name="button" id="button" class="btn btn-primary">
            Submit
        </button>
    </div>
</form>

3. Get the Values

After the form has been submitted we need to get its values from PHP's $_POST array. To do this, just enter the form labels into the validate() method and assign it to a variable.

$data = $form->validate('Name, Email, Comments');

Full Example

Copy and paste the following code into a new .php document and see just how fast and easy Formr really is.

A Full Contact Form using Bulma

<?php
// include Formr
require_once 'vendor/autoload.php';

// create our form object and use Bulma as our form wrapper
$form = new Formr\Formr('bulma');

// make all fields required
$form->required = '*';

// check if the form has been submitted
if ($form->submitted())
{
    // get our form values and assign them to a variable
    $data = $form->validate('Name, Email, Comments');

    // show a success message if no errors
    if($form->ok()) {
        $form->success_message = "Thank you, {$data['name']}!";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.1/css/bulma.min.css">
<body class="container">
    <?php $form->messages(); ?>
    <?php $form->create_form('Name, Email, Comments|textarea'); ?>
</body>
</html>