PHP Classes
elePHPant
Icontem

HTML Forms: Generate and validate submitted HTML forms

Recommend this page to a friend!
  Info   View files Documentation   View files View files (88)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2017-12-27 (1 month ago) RSS 2.0 feedNot enough user ratingsTotal: 143 This week: 10All time: 8,546 This week: 72Up
Version License PHP version Categories
delboy1978uk-form 1.0.0MIT/X Consortium ...5HTML, PHP 5, Validation
Description Author

This package can generate and validate submitted HTML forms.

It provides several classes that abstract each type of form field and can create form field objects that can be composed to generate the form output.

The package provides also means to filter and validate the submitted form input values.

The form output can be rendered using bootstrap CSS styles.

Name: Derek McLean <contact>
Classes: 1 package by
Country: United Kingdom United Kingdom
Age: ???
All time rank: 4032172 in United Kingdom United Kingdom
Week rank: 73 Up4 in United Kingdom United Kingdom Up

Details

form

Latest Stable Version Total Downloads Latest Unstable Version License<br /> Build Status Code Coverage Scrutinizer Code Quality<br /> A super easy Bootstrap ready HTML form generator for PHP

An example generated form

v1.1.0 Now has 'Dynamic Form' functionality. You can add child forms to fields like radio buttons, and have the form appear on selecting the trigger value. See the Dynamic Form section of this readme.

Installation

Install via composer

 composer require delboy1978uk/form

Usage

Firstly, "use" all the classes you'll need for your form. Then create your form and fields.

<?php

use Del\Form\Form;
use Del\Form\Field\Text;
use Del\Form\Field\CheckBox;
use Del\Form\Field\Submit;

// Create a form
$form = new Form('registration');

// Create a username, email, spamlist checkbox, and submit button.
$userName = new Text('username');
$email = new Text('email');
$spamMe = new CheckBox('spam');
$submit = new Submit('submit');

// Set labels
$userName->setLabel('User Name');
$email->setLabel('Email Address');
$spamMe->setLabel('Join our (and 3rd parties) email list(s)');

// Add the fields to the form
$form->addField($userName)
      ->addField($email)
      ->addField($spamMe)
      ->addField($submit);

// Render the form
echo $form->render();

Creating Custom Forms

Of course, it's nicer to create your own form than build one up every time, so just create a class and extend Del\Form\AbstractForm and add your fields in the init() function:

<?php
namespace My\Cool;

use Del\Form\AbstractForm;
use Del\Form\Field\Text\EmailAddress;
use Del\Form\Field\Text\Password;
use Del\Form\Field\Submit;

class LoginForm extends AbstractForm
{
    public function init() 
    {
        $email = new EmailAddress('email');   
        $password = new Password('passord');
        $submit = new Submit('submit');
        $this->addField($email);
        $this->addField($password);
        $this->addField($submit);
    }
}

Then using your form is as simple as:

<?php
use My\Cool\LoginForm;

$form = new LoginForm('login');
$form->render();

Fitering and validating input

For filtering input, add a Del\Form\Filter\Interface to your field object. For validating the filtered input, add a Del\Form\Validator\ValidatorInterface. Currently there is an adapter for Zend\Filter and Zend\Validate, but feel free to write an adapter for you favourite library. Setting a required field adds a Del\Form\Validator\NotEmpty validator.

<?php

// A text field, and an adapter for the filters and the validators
use Del\Form\Field\Text;
use Del\Form\Filter\Adapter\FilterAdapterZf;
use Del\Form\Validator\Adapter\ValidatorAdapterZf;

// Some sensible default string filters for username/email fields 
use Zend\Filter\StripTags;
use Zend\Filter\StringTrim;
use Zend\Filter\StringToLower;

// Validation rules
use Zend\Validator\CreditCard;;

// Create the field
$creditCard = new Text('credit-card');

// Create the filters
$stripTags = new FilterAdapterZf(new StripTags());
$trim = new FilterAdapterZf(new StringTrim());
$lowerCase = new FilterAdapterZf(new StringToLower());

// Create the validators
$emailAddress = new ValidatorAdapterZf(new CreditCard());

// Add them to the field
$creditCard->addFilter($stripTags)
      ->addFilter($trim)
      ->addFilter($lowerCase)
      ->addValidator($emailAddress);

Setting and getting values

Del\Form\FormInterface has a populate method which takes an array (usually the post data, but not necessarily ;-).

<?php
if (isset($_POST['submit'])) { // or ask your request object ;-) 
    $data = $_POST;
    $form->populate($data);
    if ($form->isValid()) {
        $filteredData = $form->getValues();
    }
}

After populate has been called, if you call Form::render(), it will display any validation error messages.

Field Types

Text

Del\Form\Field\Text fields are the most basic field, and come with a built in StripTags and StringTrim filter.

Del\Form\Field\Text\EmailAddress extends Text, and adds an EmailAddress validator for convenience.

Del\Form\Field\Text\Password is a password field which also extends Text.

<?php
use Del\Form\Field\Text;

$text = new Text('text');
$text->setLabel('Needed Details');
$text->setRequired(true);
$text->setPlaceholder('type some text..');
$text->setValue('Blah');

TextArea

Del\Form\Field\TextArea fields are pretty much the same as the Text field

<?php
use Del\Form\Field\TextArea;

$textArea = new TextArea('message');;

Select

Del\Form|Field\Select needs initialised with setOptions():

<?php
use Del\Form\Field\Select;

$select = new Select('choose');
$select->setOptions([
    'BK' => 'Burger King',
    'McD' => 'McDonalds',
    'Q' => 'Quick',
]);

Radio

Del\Form|Field\Radio can be rendered inline (side by side) or not, also needs initialised with setOptions():

<?php
use Del\Form\Field\Radio;

$radio = new Radio('choose');
$radio->setRenderInline(true);
$radio->setOptions([
    'BK' => 'Burger King',
    'McD' => 'McDonalds',
    'Q' => 'Quick',
]);

Checkbox

Del\Form|Field\Checkbox can be rendered inline or not, also needs initialised with setOptions():

<?php
use Del\Form\Field\CheckBox;

$check = new CheckBox('choose');
$check->setOptions([
    'BK' => 'Burger King',
    'McD' => 'McDonalds',
    'Q' => 'Quick',
]);

FileUpload

Del\Form|Field\FileUpload fields come with a nifty looking Bootstrap compatible renderer, but it uses some javascript. If you don't want that, just call setRenderer() and pass it a default TextRender class instance.

<?php
use Del\Form\Field\FileUpload;

$fileUpload = new FileUpload('photo');
$fileUpload->setUploadDirectory('/path/to/destination');

Submit

Del\Form|Field\Submit doesn't really need much:

<?php
use Del\Form\Field\Submit;

$submit = new Submit('submit');
$submit->setValue('Send');

Dynamic Forms

You can have for example a radio button with two choices, and each choice can have a separate form relevant to the checked value. For instance, a radio with a choice of food or drink could then display a dynamic form upon clicking the drink option, and the drinks form will display.

An example generated form

<?php

$radio = new Radio('choice');
$radio->setLabel('Please choose..');
$radio->setRenderInline(true);
$radio->setRequired(true);
$radio->setOptions([
    1 => 'Food',
    2 => 'Drink',
]);

$foodForm = new Form('food');  // This form appears when radio choice 1 is selected
$foodRadio = new Radio('foodchoice');
$foodRadio->setLabel('Choose your food.');
$foodRadio->setRequired(true);
$foodRadio->setOptions([
    1 => 'Cheeseburger',
    2 => 'Pizza',
    3 => 'Steak',
]);
$foodForm->addField($foodRadio);
$radio->addDynamicForm($foodForm, 1);

$drinkForm = new Form('drink');  // This form appears when radio choice 2 is selected
$drinkRadio = new Radio('drinkchoice');
$drinkRadio->setRequired(true);
$drinkRadio->setLabel('Choose your drink.');
$drinkRadio->setOptions([
    1 => 'Beer',
    2 => 'Vodka',
    3 => 'Whisky',
]);
$moreText = new Text('moretext');
$moreText->setLabel('whatever');
$moreText->setPlaceholder('Another text field to fill in');
$drinkForm->addField($drinkRadio);
$drinkForm->addField($moreText);
$radio->addDynamicForm($drinkForm, 2);

  Files folder image Files  
File Role Description
Files folder imagesrc (3 files, 6 directories)
Files folder imagetests (4 files, 5 directories)
Accessible without login Plain text file .scrutinizer.yml Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file codeception.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Class source

  Files folder image Files  /  src  
File Role Description
Files folder imageCollection (5 files)
Files folder imageField (11 files, 1 directory)
Files folder imageFilter (1 file, 1 directory)
Files folder imageRenderer (4 files, 2 directories)
Files folder imageTraits (4 files)
Files folder imageValidator (3 files, 1 directory)
  Accessible without login Plain text file AbstractForm.php Class Class source
  Accessible without login Plain text file Form.php Class Class source
  Accessible without login Plain text file FormInterface.php Class Class source

  Files folder image Files  /  src  /  Collection  
File Role Description
  Accessible without login Plain text file AbstractCollection.php Class Class source
  Accessible without login Plain text file CollectionInterface.php Class Class source
  Accessible without login Plain text file FieldCollection.php Class Class source
  Accessible without login Plain text file FilterCollection.php Class Class source
  Accessible without login Plain text file ValidatorCollection.php Class Class source

  Files folder image Files  /  src  /  Field  
File Role Description
Files folder imageText (2 files)
  Accessible without login Plain text file ArrayValueInterface.php Class Class source
  Accessible without login Plain text file CheckBox.php Class Class source
  Accessible without login Plain text file FieldAbstract.php Class Class source
  Accessible without login Plain text file FieldInterface.php Class Class source
  Accessible without login Plain text file FileUpload.php Class Class source
  Accessible without login Plain text file Hidden.php Class Class source
  Accessible without login Plain text file Radio.php Class Class source
  Accessible without login Plain text file Select.php Class Class source
  Accessible without login Plain text file Submit.php Class Class source
  Accessible without login Plain text file Text.php Class Class source
  Accessible without login Plain text file TextArea.php Class Class source

  Files folder image Files  /  src  /  Field  /  Text  
File Role Description
  Accessible without login Plain text file EmailAddress.php Class Class source
  Accessible without login Plain text file Password.php Class Class source

  Files folder image Files  /  src  /  Filter  
File Role Description
Files folder imageAdapter (1 file)
  Accessible without login Plain text file FilterInterface.php Class Class source

  Files folder image Files  /  src  /  Filter  /  Adapter  
File Role Description
  Accessible without login Plain text file FilterAdapterZf.php Class Class source

  Files folder image Files  /  src  /  Renderer  
File Role Description
Files folder imageError (4 files)
Files folder imageField (8 files)
  Accessible without login Plain text file AbstractFormRenderer.php Class Class source
  Accessible without login Plain text file FormRenderer.php Class Class source
  Accessible without login Plain text file FormRendererInterface.php Class Class source
  Accessible without login Plain text file HorizontalFormRenderer.php Class Class source

  Files folder image Files  /  src  /  Renderer  /  Error  
File Role Description
  Accessible without login Plain text file AbstractErrorRender.php Class Class source
  Accessible without login Plain text file DefaultErrorRender.php Class Class source
  Accessible without login Plain text file ErrorRendererInterface.php Class Class source
  Accessible without login Plain text file HorizontalFormErrorRender.php Class Class source

  Files folder image Files  /  src  /  Renderer  /  Field  
File Role Description
  Accessible without login Plain text file AbstractFieldRender.php Class Class source
  Accessible without login Plain text file CheckboxRender.php Class Class source
  Accessible without login Plain text file FieldRendererInterface.php Class Class source
  Accessible without login Plain text file FileUploadRender.php Class Class source
  Accessible without login Plain text file RadioRender.php Class Class source
  Accessible without login Plain text file SelectRender.php Class Class source
  Accessible without login Plain text file TextAreaRender.php Class Class source
  Accessible without login Plain text file TextRender.php Class Class source

  Files folder image Files  /  src  /  Traits  
File Role Description
  Accessible without login Plain text file CanRenderInlineTrait.php Class Class source
  Accessible without login Plain text file HasAttributesTrait.php Class Class source
  Accessible without login Plain text file HasDomTrait.php Class Class source
  Accessible without login Plain text file HasOptionsTrait.php Class Class source

  Files folder image Files  /  src  /  Validator  
File Role Description
Files folder imageAdapter (1 file)
  Accessible without login Plain text file FloatValidator.php Class Class source
  Accessible without login Plain text file NotEmpty.php Class Class source
  Accessible without login Plain text file ValidatorInterface.php Class Class source

  Files folder image Files  /  src  /  Validator  /  Adapter  
File Role Description
  Accessible without login Plain text file ValidatorAdapterZf.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageacceptance (2 files)
Files folder imagefunctional (2 files)
Files folder imageunit (2 files, 1 directory)
Files folder image_data (4 files)
Files folder image_support (6 files, 1 directory)
  Accessible without login Plain text file acceptance.suite.yml Data Auxiliary data
  Accessible without login Plain text file functional.suite.yml Data Auxiliary data
  Accessible without login Plain text file unit.suite.yml Data Auxiliary data
  Accessible without login Plain text file _bootstrap.php Aux. Auxiliary script

  Files folder image Files  /  tests  /  acceptance  
File Role Description
  Accessible without login Plain text file AcceptanceTester.php Class Class source
  Accessible without login Plain text file _bootstrap.php Aux. Auxiliary script

  Files folder image Files  /  tests  /  functional  
File Role Description
  Accessible without login Plain text file FunctionalTester.php Class Class source
  Accessible without login Plain text file _bootstrap.php Aux. Auxiliary script

  Files folder image Files  /  tests  /  unit  
File Role Description
Files folder imageDel (2 files, 3 directories)
  Accessible without login Plain text file UnitTester.php Class Class source
  Accessible without login Plain text file _bootstrap.php Aux. Auxiliary script

  Files folder image Files  /  tests  /  unit  /  Del  
File Role Description
Files folder imageField (7 files)
Files folder imageRenderer (1 file)
Files folder imageValidator (1 file)
  Accessible without login Plain text file DynamicFormTest.php Class Class source
  Accessible without login Plain text file FormTest.php Class Class source

  Files folder image Files  /  tests  /  unit  /  Del  /  Field  
File Role Description
  Accessible without login Plain text file CheckboxTest.php Class Class source
  Accessible without login Plain text file FileUploadTest.php Class Class source
  Accessible without login Plain text file HiddenTest.php Class Class source
  Accessible without login Plain text file RadioTest.php Class Class source
  Accessible without login Plain text file SelectTest.php Class Class source
  Accessible without login Plain text file TextAreaTest.php Class Class source
  Accessible without login Plain text file TextTest.php Class Class source

  Files folder image Files  /  tests  /  unit  /  Del  /  Renderer  
File Role Description
  Accessible without login Plain text file HorizontalFormRendererTest.php Class Class source

  Files folder image Files  /  tests  /  unit  /  Del  /  Validator  
File Role Description
  Accessible without login Plain text file FloatValidatorTest.php Class Class source

  Files folder image Files  /  tests  /  _data  
File Role Description
  Accessible without login Plain text file dump.sql Data Auxiliary data
  Accessible without login Image file dynamic-form.PNG Data Auxiliary data
  Accessible without login Image file fol.gif Icon Icon image
  Accessible without login Image file form.png Data Auxiliary data

  Files folder image Files  /  tests  /  _support  
File Role Description
Files folder image_generated (3 files)
  Accessible without login Plain text file AcceptanceHelper.php Class Class source
  Accessible without login Plain text file AcceptanceTester.php Class Class source
  Accessible without login Plain text file FunctionalHelper.php Class Class source
  Accessible without login Plain text file FunctionalTester.php Class Class source
  Accessible without login Plain text file UnitHelper.php Class Class source
  Accessible without login Plain text file UnitTester.php Class Class source

  Files folder image Files  /  tests  /  _support  /  _generated  
File Role Description
  Accessible without login Plain text file AcceptanceTesterActions.php Class Class source
  Accessible without login Plain text file FunctionalTesterActions.php Class Class source
  Accessible without login Plain text file UnitTesterActions.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:143
This week:10
All time:8,546
This week:72Up