Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/formr/formr

Create and Validate PHP Forms in Seconds.
https://github.com/formr/formr

bootstrap bulma form form-builder form-class form-library form-validation forms honeypot open-source php-form php-form-builder php-validation php-validator recaptcha validation

Last synced: 3 days ago
JSON representation

Create and Validate PHP Forms in Seconds.

Awesome Lists containing this project

README

        

# Formr

[![Latest Version on Packagist][ico-version]][link-packagist]
[![Total Downloads][ico-downloads]][link-downloads]

Formr is a ridiculously fast and easy PHP form builder, with support for Bootstrap and Bulma right out of the box!

Find docs here: [http://formr.github.io](http://formr.github.io)

If you find Formr useful, please consider starring the project and/or making a [donation](https://paypal.me/timgavin). Thank you!

## Features

- Create complex forms with server-side processing and validation in seconds
- Built-in support for Bootstrap, Bulma, Tailwind and Uikit
- 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

```bash
composer require formr/formr
```

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

```php
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.

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

## Bootstrap & Bulma Ready

Bootstrap and Bulma form classes are ready to go! Just tell Formr you want to use Bootstrap or Bulma when creating a new form and Formr will take care of the rest.

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

```php
$form = new Formr\Formr('bulma');
```

## Basic Example

Simply enter your form labels as a comma delimited string and Formr will build the form, complete with opening and closing tags, a submit button, and email validation - plus all values retained upon `POST`. Easy!

```php
$form = new Formr\Formr('bootstrap');
$form->create_form('Name, Email, Comments|textarea');
```

### Produces the following HTML

```html



Name




Email




Comments




Submit

```

## Basic Example with More Control

Using the `create()` method tells Formr you want control over adding the form tags and submit button yourself. Otherwise it's the same as the Basic Example above.

```php
$form = new Formr\Formr('bootstrap');
$form->form_open();
$form->create('First name, Last name, Email address, Age|number, Comments|textarea');
$form->submit_button();
$form->form_close();
```

#### Produces the following HTML

```html



First name





Last name





Email address





Age





Comments





Submit

```

## Pre-Built Forms

Formr has several common forms already baked in, and it's really easy to [create and save your own](https://github.com/formr/extend).

```php
$form = new Formr\Formr();
$form->fastform('contact');
```

#### Produces the following HTML

```html



First name:


Last name:


Email:


Comments:


```

## Build Forms With Arrays

```php
$data = [
'text' => 'name, Name:',
'email' => 'email, Email:',
'checkbox' => 'agree, I Agree',
];

$form = new Formr\Formr('bootstrap');
$form->fastform($data);
```

#### Produces the following HTML

```html




Name:




Email:




I Agree





```

## Build Forms in HTML

You have full control over how you build your forms...

```html


text('name', 'Name'); ?>


email('email', 'Email address', '[email protected]', 'emailID', 'placeholder="[email protected]"'); ?>




```

#### Produces the following HTML

```html



Name




Email address



```

## Retrieving POST Values

It's super easy to retrieve your `$_POST` values and assign them to variables!

```php
$name = $form->post('name');
$email = $form->post('email');
```

## Validation

#### Formr can easly process and validate your forms

Like the `create()` method, we can pass a list of our form labels to the `validate()` method, which will get the `$_POST` values of our form fields and put them into an array. If your field name is `email`, a `valid_email` validation rule will be applied automatically!

#### Basic usage

```php
$form->validate('Name, Email, Comments');
```

Let's make sure the form was submitted, then we'll validate and get the value of our email field from the array.

```php
if($form->submitted()) {
$data = $form->validate('Name, Email, Comments');
$email = $data['email'];
}
```

#### Adding Rules

Let's make sure `Name` is a minimum of 2 characters and a maximum of 30 by adding our validation rules wrapped in parentheses.

```php
$form->validate('Name(min[2]|max[30]), Email, Comments');
```

## Fine-Tune Your Validation

Of course you can get more in-depth with your validation, and even add custom error messaging! The following is a basic example, however Formr's validation methods are quite powerful and include, among other things, comparing values between fields and hashing using `bcrypt()`

Let's get the value of our `email` field using the `post()` method.

```php
$email = $form->post('email');
```

Now let's make sure it's a valid email address by entering the `valid_email` validation rule in the third parameter. If there's an error, the text entered into the second parameter will notify the user to correct the `Email` field.

```php
$email = $form->post('email','Email','valid_email');
```

We can take that a step further and enter a full custom error message in the second parameter to make our forms even more user-friendly.

```php
$email = $form->post('email','Email|Please enter a valid email address','valid_email');
```

## Full Example

```php
required = '*';

// check if the form has been submitted
if($form->submitted())
{
// make sure our Message field has at least 10 characters
$form->validate('Message(min[10])');

// let's email the form
$to = '[email protected]';
$from = '[email protected]';
$subject = 'Contact Form Submission';

// this processes our form, cleans the input, and sends it as an HTML email
if($form->send_email($to, $subject, 'POST', $from, 'HTML'))
{
// email sent; print a thank you message
$form->success_message('Thank you for filling out our form!');
}
}
?>


Formr


messages();

// create the form
$form->create_form('First name, Last name, Email address, Message|textarea');
?>

```
[ico-version]: https://img.shields.io/packagist/v/formr/formr.svg?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/formr/formr.svg?style=flat-square

[link-packagist]: https://packagist.org/packages/formr/formr
[link-downloads]: https://packagist.org/packages/formr/formr
[link-author]: https://github.com/timgavin