2017 © Pedro Peláez
 

library php-html-form

A basic framework agnostic form building package with a few extra niceties like remembering old input and retrieving error messages.

image

bupy7/php-html-form

A basic framework agnostic form building package with a few extra niceties like remembering old input and retrieving error messages.

  • Saturday, November 4, 2017
  • by bupy7
  • Repository
  • 1 Watchers
  • 0 Stars
  • 3,826 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 89 Forks
  • 0 Open issues
  • 33 Versions
  • 24 % Grown

The README.md

php-html-form

This is fork of adamwathan/form. There is just HTML-elements builder without any framework support. Extension are supporting PHP from 5.6 to 8.x!, (*1)

Stable Version Build status Coverage Status Total Downloads License, (*2)

Boring name for a boring package. Builds form HTML with a fluent-ish, hopefully intuitive syntax., (*3)

Installation

You can install this package via Composer by running this command in your terminal in the root of your project:, (*4)

$ composer require bupy7/php-html-form

Basic Usage

Getting Started

First, instantiate a FormBuilder..., (*5)

$builder = new AdamWathan\Form\FormBuilder;

Next, use the FormBuilder to build an element. For example:, (*6)

// <input type="text" name="email" value="example@example.com" required="required">
<?= $builder->text('email')->value('example@example.com')->required(); ?>
  • All elements support method chaining, so you can add as many options to an element as you need.
  • All elements implement __toString() so there is no need to manually render.

Opening a Form

// 

<

form method="POST">
= $builder->open(); ?>

// 

<

form method="GET">
= $builder->open()->get(); ?>

// 

<

form method="POST">
// <input type="hidden" name="_method" value="PUT">
= $builder->open()->put(); ?>

// 

<

form method="POST">
// <input type="hidden" name="_method" value="DELETE">
= $builder->open()->delete(); ?>

// 

<

form method="POST" action="/test">
= $builder->open()->action('/test'); ?>

// 

<

form method="POST" action="" enctype="multipart/form-data">
= $builder->open()->multipart() ?>

// 

<

form method="POST" action="" enctype="custom">
= $builder->open()->encodingType("custom") ?>

Text and Password Fields

Text and password fields share the same interface., (*7)

// <input type="text" name="email">
= $builder->text('email'); ?>

// <input type="text" name="email" id="email_field">
= $builder->text('email')->id('email_field'); ?>

// <input type="password" name="password" class="required">
= $builder->password('password')->addClass('required'); ?>

// <input type="text" name="email" value="example@example.com" required="required">
= $builder->text('email')->value('example@example.com')->required(); ?>

Other available methods:, (*8)

  • placeholder($string)
  • optional()
  • defaultValue($string)
  • disable()
  • enable()

Textareas

Textareas share the same interface as regular text fields, with a couple of extra useful methods., (*9)

// <textarea name="bio" rows="5" cols="50"></textarea>
= $builder->textarea('bio')->rows(5); ?>

// <textarea name="bio" rows="10" cols="20"></textarea>
= $builder->textarea('bio')->cols(20); ?>

// <textarea name="bio" rows="5" cols="20" class="important">My biography</textarea>
= $builder->textarea('bio')->rows(5)->cols(20)->addClass('important')->value('My biography'); ?>

Checkboxes and Radio Buttons

// <input type="checkbox" name="terms" value="1">
= $builder->checkbox('terms'); ?>

// <input type="checkbox" name="terms" value="1" checked="checked">
= $builder->checkbox('terms')->check(); ?>

// <input type="checkbox" name="terms" value="1">
= $builder->checkbox('terms')->uncheck(); ?>

// <input type="checkbox" name="terms" value="1" checked="checked">
= $builder->checkbox('terms')->defaultToChecked(); ?>

// <input type="checkbox" name="terms" value="agree">
= $builder->checkbox('terms')->value('agree'); ?>

// <input type="radio" name="color" value="red">
= $builder->radio('color', 'red'); ?>

Selects

// <select name="birth_year"></select>
= $builder->select('birth_year'); ?>

// <select name="birth_year">
//   <option value="0">1990</option>
//   <option value="1">1991</option>
//   <option value="2">1992</option>
// </select>
= $builder->select('birth_year', [1990, 1991, 1992]); ?>

// <select name="birth_year">
//   <option value="1990">1990</option>
//   <option value="1991">1991</option>
//   <option value="1992">1992</option>
// </select>
= $builder->select('birth_year', ['1990' => 1990, '1991' => 1991, '1992' => 1992]); ?>

// <select name="birth_year">
//   <optgroup label="Ontario">
//     <option value="toronto">Toronto</option>
//     <option value="ottawa">Ottawa</option>
//   </optgroup>
//   <optgroup label="Quebec">
//     <option value="montreal">Montreal</option>
//     <option value="quebec_city">Quebec City</option>
//   </optgroup>
// </select>
$options = [
    'Ontario' => [
        'toronto' => 'Toronto',
        'ottawa' => 'Ottawa',
    ],
    'Quebec' => [
        'montreal' => 'Montreal',
        'quebec_city' => 'Quebec City',
    ]
];

= $builder->select('birth_year', $options); ?>

// <select name="birth_year">
//   <option value="1">1990</option>
// </select>
= $builder->select('birth_year')->addOption('1', 1990); ?>

// <select name="birth_year">
//   <option value="1">1990</option>
//   <option value="2">1991</option>
//   <option value="3" selected>1992</option>
// </select>
= $builder->select('birth_year', ['1' => 1990, '2' => 1991, '3' => 1992])->select('3'); ?>

Buttons

// <button type="button">Click Me</button>
= $builder->button('Click Me'); ?>

// <button type="submit">Sign Up</button>
= $builder->submit('Sign Up'); ?>

// <button type="reset">Reset Form</button>
= $builder->reset('Reset Form'); ?>

// <button type="submit" class="js-submit">Sign Up</button>
= $builder->submit('Sign Up')->addClass('js-submit'); ?>

Hidden Inputs

// <input type="hidden" name="secret" value="my-secret-value">
<?= $builder->hidden('secret')->value('my-secret-value'); ?>

Labels

Basic Label, (*10)

// <label>Email</label>
= $builder->label('Email'); ?>

// <label for="email">Email</label>
= $builder->label('Email')->forId('email'); ?>

Wrapping another element, (*11)

// <label>Email<input type="text" name="email"></label>
= $builder->label('Email')->before($emailElement); ?>

// <label><input type="text" name="email">Email</label>
= $builder->label('Email')->after($emailElement); ?>

Setting Attributes

// Attributes can be set with attribute(...)
// <input type="text" name="foobar" min="4">
= $builder->text('foobar')->attribute('min', 4); ?>

// Or by calling the attribute name as a method
// <input type="text" name="foobar" min="4">
= $builder->text('foobar')->min(4); ?>

// Setting data-* attributes
// <input type="text" data-foo="bar" name="foobar">
= $builder->text('foobar')->data('foo', 'bar'); ?>

// Multiple data-* attributes can be set at once
// <input type="text" data-foo="bar" data-bar="foo" name="foobar">
= $builder->text('foobar')->data(['foo' => 'bar', 'bar' => 'foo']); ?>

Error Messages

FormBuilder also allows you to easily retrieve error messages for your form elements. To do so, just implement the ErrorStoreInterface and pass it to the FormBuilder:, (*12)

$builder->setErrorStore($myErrorStore);

// Check if the form has an error for an element
$builder->hasError('email');

// Retrieve the error message for an element
$builder->getError('email');

You can also supply a format parameter to getError() to cleanup your markup. Instead of doing this:, (*13)

<?php if ($builder->hasError('email')): ?>
    <span class="error"><?= $builder->getError('email'); ?></span>
<?php endif; ?>

...you can simply do this, which will display the formatted message if it exists, or nothing otherwise., (*14)

<?= $builder->getError('email', '<span class="error">:message</span'); ?>

, (*15)

CSRF Protection

Assuming you set a CSRF token when instantiating the FormBuilder, add a CSRF token to your form easily like so:, (*16)

<?= $builder->token(); ?>

Data Binding

Sometimes you might have a form where all of the fields match properties on some sort of object or array in your system, and you want the user to be able to edit that data. Data binding makes this really easy by allowing you to bind an object or array to your form that will be used to automatically provide all of the default values for your fields., (*17)

$model->first_name = "John";
$model->last_name = "Doe";
$model->email = "john@example.com";

= $builder->open(); ?>
= $builder->bind($model); ?>
= $builder->text('first_name'); ?>
= $builder->text('last_name'); ?>
= $builder->email('email'); ?>
= $builder->close(); ?>

Note: Be sure to bind before creating any other form elements., (*18)

Testing

Run tests:, (*19)

$ ./vendor/bin/phpunit --no-coverage

Run tests with coverage:, (*20)

$ XDEBUG_MODE=coverage ./vendor/bin/phpunit

HTML coverage path: build/coverage/index.html, (*21)

Code style

To fix code style, run:, (*22)

~/.composer/vendor/bin/php-cs-fixer fix --config=./phpcs.php --verbose

You have to install PHP CS Fixer at first, if you don't use build-in Docker image:, (*23)

composer global require friendsofphp/php-cs-fixer

License

php-html-form is released under the MIT License., (*24)

The Versions

04/11 2017

dev-master

9999999-dev

A basic framework agnostic form building package with a few extra niceties like remembering old input and retrieving error messages.

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Adam Wathan
by Vasily Velosloodcev

04/11 2017

1.0.1

1.0.1.0

A basic framework agnostic form building package with a few extra niceties like remembering old input and retrieving error messages.

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Adam Wathan
by Vasily Velosloodcev

24/09 2017

1.0.0

1.0.0.0

A basic framework agnostic form building package with a few extra niceties like remembering old input and retrieving error messages.

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Adam Wathan
by Vasily Velosloodcev

24/09 2017

0.10.0

0.10.0.0

A basic framework agnostic form building package with a few extra niceties like remembering old input and retrieving error messages.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Adam Wathan
by Vasily Velosloodcev

07/07 2017

v0.9.0

0.9.0.0

A basic framework agnostic form building package with a few extra niceties like remembering old input and retrieving error messages.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Adam Wathan

04/03 2017

v0.8.13

0.8.13.0

A basic framework agnostic form building package with a few extra niceties like remembering old input and retrieving error messages.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Adam Wathan

31/01 2017

v0.8.12

0.8.12.0

A basic framework agnostic form building package with a few extra niceties like remembering old input and retrieving error messages.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Adam Wathan

19/01 2017

v0.8.11

0.8.11.0

A basic framework agnostic form building package with a few extra niceties like remembering old input and retrieving error messages.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Adam Wathan

20/09 2016

v0.8.10

0.8.10.0

A basic framework agnostic form building package with a few extra niceties like remembering old input and retrieving error messages.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Adam Wathan

10/05 2016

v0.8.9

0.8.9.0

A basic framework agnostic form building package with a few extra niceties like remembering old input and retrieving error messages.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Adam Wathan

02/04 2016

v0.8.8

0.8.8.0

A basic framework agnostic form building package with a few extra niceties like remembering old input and retrieving error messages.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Adam Wathan

28/03 2016

v0.8.7

0.8.7.0

A basic framework agnostic form building package with a few extra niceties like remembering old input and retrieving error messages.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Adam Wathan

25/03 2016

v0.8.6

0.8.6.0

A basic framework agnostic form building package with a few extra niceties like remembering old input and retrieving error messages.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Adam Wathan

25/03 2016

v0.8.5

0.8.5.0

A basic framework agnostic form building package with a few extra niceties like remembering old input and retrieving error messages.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Adam Wathan

25/03 2016

v0.8.4

0.8.4.0

A basic framework agnostic form building package with a few extra niceties like remembering old input and retrieving error messages.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Adam Wathan

23/03 2016

v0.8.3

0.8.3.0

A basic framework agnostic form building package with a few extra niceties like remembering old input and retrieving error messages.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Adam Wathan

19/02 2016

v0.8.2

0.8.2.0

A basic framework agnostic form building package with a few extra niceties like remembering old input and retrieving error messages.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Adam Wathan

19/02 2016

v0.8.1

0.8.1.0

A basic framework agnostic form building package with a few extra niceties like remembering old input and retrieving error messages.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Adam Wathan

10/10 2015

v0.8.0

0.8.0.0

A basic framework agnostic form building package with a few extra niceties like remembering old input and retrieving error messages.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Adam Wathan

07/08 2015

v0.7.2

0.7.2.0

A basic framework agnostic form building package with a few extra niceties like remembering old input and retrieving error messages.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Adam Wathan

10/06 2015

v0.7.1

0.7.1.0

A basic framework agnostic form building package with a few extra niceties like remembering old input and retrieving error messages.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Adam Wathan

30/05 2015

v0.7.0

0.7.0.0

A basic framework agnostic form building package with a few extra niceties like remembering old input and retrieving error messages.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Adam Wathan

23/05 2015

v0.6.1

0.6.1.0

A basic framework agnostic form building package with a few extra niceties like remembering old input and retrieving error messages.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Adam Wathan

30/04 2015

v0.6.0

0.6.0.0

A basic framework agnostic form building package with a few extra niceties like remembering old input and retrieving error messages.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Adam Wathan

28/04 2015

v0.5.2

0.5.2.0

A basic framework agnostic form building package with a few extra niceties like remembering old input and retrieving error messages.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Adam Wathan

20/02 2015

v0.5.1

0.5.1.0

A basic framework agnostic form building package with a few extra niceties like remembering old input and retrieving error messages.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Adam Wathan

19/02 2015

v0.5.0

0.5.0.0

A basic framework agnostic form building package with a few extra niceties like remembering old input and retrieving error messages.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Adam Wathan

29/09 2014

v0.4.0

0.4.0.0

A basic framework agnostic form building package with a few extra niceties like remembering old input and retrieving error messages.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Adam Wathan

12/09 2014

v0.3.2

0.3.2.0

A basic framework agnostic form building package with a few extra niceties like remembering old input and retrieving error messages.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Adam Wathan

23/08 2014

v0.3.1

0.3.1.0

A basic framework agnostic form building package with a few extra niceties like remembering old input and retrieving error messages.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Adam Wathan

12/08 2014

v0.3.0

0.3.0.0

A basic framework agnostic form building package with a few extra niceties like remembering old input and retrieving error messages.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Adam Wathan

21/03 2014

v0.2

0.2.0.0

A basic framework agnostic form building package with a few extra niceties like remembering old input and retrieving error messages.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Adam Wathan

09/10 2013

v0.1

0.1.0.0

A basic framework agnostic form building package with a few extra niceties like remembering old input and retrieving error messages.

  Sources   Download

The Requires

  • php >=5.3.0

 

The Development Requires

by Adam Wathan