PHPackages                             formhandler/formhandler - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. formhandler/formhandler

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

formhandler/formhandler
=======================

Easy to use object which allows you to create and process forms

v5(4y ago)2271910[5 issues](https://github.com/FormHandler/FormHandler/issues)MITPHPPHP &gt;=7.4

Since Dec 17Pushed 3y ago8 watchersCompare

[ Source](https://github.com/FormHandler/FormHandler)[ Packagist](https://packagist.org/packages/formhandler/formhandler)[ RSS](/packages/formhandler-formhandler/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (4)Dependencies (3)Versions (5)Used By (0)

[![Build Status](https://camo.githubusercontent.com/7a68f4c1f5723cc0342dd0de1310486ef06923a1ca915648df861502d8d65267/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f466f726d48616e646c65722f466f726d48616e646c65722f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/FormHandler/FormHandler/build-status/master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/e75cf84c0770963e9202a149383615ab67ec6fb49d220f14ae7114cb58faf1ce/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f466f726d48616e646c65722f466f726d48616e646c65722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/FormHandler/FormHandler/?branch=master)[![Minimum PHP Version](https://camo.githubusercontent.com/eb773fa94283cbea6c7b192d460983a781ae4a16409af56d109fe3e8e71ab6c9/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344253230372e342d3838393242462e7376673f7374796c653d666c61742d737175617265)](https://php.net/)[![Code Coverage](https://camo.githubusercontent.com/2d7c2d30de439fbe87376b94974cae4282238e88eef64cdd80ff47c8e39fffb3/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f466f726d48616e646c65722f466f726d48616e646c65722f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/FormHandler/FormHandler/?branch=master)[![Packagist Version](https://camo.githubusercontent.com/446cecf25f5a282c0e794e634d34f462a686770601862e9175772df9480abd41/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f666f726d68616e646c65722f666f726d68616e646c65722e737667)](https://packagist.org/packages/formhandler/formhandler)[![Total Downloads](https://camo.githubusercontent.com/eae6a905843ee46724507185ca50beb1c9a3f4a9c0300782468f3c077b9df001/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f666f726d68616e646c65722f666f726d68616e646c65722e737667)](https://packagist.org/packages/formhandler/formhandler)

FormHandler
===========

[](#formhandler)

This FormHandler is a PHP solution to generate form fields and validate them. Making forms is in general a time-taking job. In this package we try to offer a solution so that making forms is easy.

FormHandler implements the PSR-1 and PSR-2 coding standards. FormHandler implements the PSR-4 autoloading standard.

To create a form you have to:

- Define the form and its fields
- Check if the form is submitted and if it's valid
- Parse the form's fields in your HTML / view

Requirements
------------

[](#requirements)

FormHandler requires PHP 7.4 or higher.

FormHandler is tested on:

- PHP 7.4
- PHP 8.0
- PHP Latest 'nightly' build

Installation
------------

[](#installation)

You can install FormHandler by downloading the [latest](https://github.com/formhandler/formHandler/archive/master.zip)zip file and include this in your project.

We are working on availability for composer.

Usage
-----

[](#usage)

FormHandler has a few assumptions:

- A form is **always** submitted to itself. That means, to the same script/page where the form is defined.
- We assume that when you create a `SubmitButton` or `ImageButton`, that you don't use own HTML tag buttons.

A very basic example is:

```
#
# This code defines your form and what to do with it when it's valid.
# This code is probably defined in your controller.
#

// Create the form
$form = new Form();

// Create a field in the form. Fluent method chaining is supported.
$form->textField('name')
    ->addValidator(new StringValidator(2, 50, true, 'You have to supply your name (between 2 and 50 characters)'))
    ->setPlaceholder('Enter your name');

// Check if the form is submitted
if ($form->isSubmitted()) {

    // Check if the form is valid.
    if ($form->isValid()) {
        // Do your stuff here with the form, for example, store something in a database.
    }
} else {
    // Here, the form is not yet submitted!
    // You could for example set some predefined values in the form.
}

#
# Then, in your view, you can use your form fields.
#

// This will display the  html tag.
echo $form;

// This will display the HTML tag for the "name" field.
echo $form('name');

// You can mix plain old html with "dynamic" generated fields
// Of course you could also generate a SubmitButton object and use that one.
echo '';
```

So, this was our first basic example. Let's see what happens here.

- First we create the form and add a textfield called "name". We append a `StringValidator` to the field where we allow values between 2 and 50 characters.
    We define that this field is required and if the field is invalid, we use a custom error message.
- Then we check if our form is submitted. If the form is not yet submitted, you could prefill your fields with predefined values. This is usually the case for *edit* forms. On your first execution of this script, the form will not be submitted, so this part will be skipped.
- After checking if the form is submitted, we check if the form is valid. If the form is valid, you can use the submitted values and process them (for example, store them in a database). When the submitted form is invalid, you could just ignore the values. The form will be rendered again, which will display the error message to the user about incorrect form fields.
- Finally, in our view we render the HTML. You are self responsible to render the fields where you want. FormHandler does not mix with the design of your fields, except from some form related HTML tags like `label`for a radio button.

Fluent Interface
----------------

[](#fluent-interface)

FormHandler implements a fluent interface. This means that you *can* use method chaining. It's not required though.

Example:

```
