PHPackages                             tuum/form - 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. [Templating &amp; Views](/categories/templating)
4. /
5. tuum/form

ActiveLibrary[Templating &amp; Views](/categories/templating)

tuum/form
=========

form generator and other template helpers.

1.0.7(8y ago)0216[4 issues](https://github.com/TuumPHP/Form/issues)2MITPHP

Since Mar 2Pushed 8y ago1 watchersCompare

[ Source](https://github.com/TuumPHP/Form)[ Packagist](https://packagist.org/packages/tuum/form)[ RSS](/packages/tuum-form/feed)WikiDiscussions 1.x Synced 1mo ago

READMEChangelogDependenciesVersions (26)Used By (2)

Tuum/Form
=========

[](#tuumform)

[![Scrutinizer Code Quality](https://camo.githubusercontent.com/547eb06b2326b69b1e585bdcd6618dc2fa66e3975d68fe0c637caa9489593fd5/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f5475756d5048502f466f726d2f6261646765732f7175616c6974792d73636f72652e706e673f623d312e78)](https://scrutinizer-ci.com/g/TuumPHP/Form/?branch=1.x)[![Build Status](https://camo.githubusercontent.com/34a5c406efef925a28d14dc3772e2bcc35400a963c7cf0b21d69a34a962aabe6/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f5475756d5048502f466f726d2f6261646765732f6275696c642e706e673f623d312e78)](https://scrutinizer-ci.com/g/TuumPHP/Form/build-status/1.x)

Helper classes for escaping input values, generating HTML tags, and managing data.

### Licence

[](#licence)

MIT Licence

### PSR

[](#psr)

PSR-1, PSR-2, and PSR-4.

Getting Started
---------------

[](#getting-started)

### Installation

[](#installation)

```
composer require "tuum/form: ^1.0"
```

### Sample Code

[](#sample-code)

There are many helpers, and some are related. It would be easier to use `DataView` object to manage helpers. For instance,

```
$view = new Tuum\Form\DataView();
$view->setData([
    'name' => 'my-name',
    'bold' => 'bold',
    'list' => ['v1', 'v2'],
]);
$view->setInputs([
    'name' => 'old-name',
    'more' => [
        'key' => 'val'
    ]
]);
// getting escaped data.
echo $view->data['name']; // 'my-name'
echo $view->data['bold']; // escaped bold
echo $view->data->get('bold'); //  escaped bold
echo $view->data->raw('bold'); //  raw bold
// and arrays.
$list = $view->data->extractKey('list');
echo $list[0]; // 'v1'
echo $list[1]; // 'v2'

// getting old inputs
echo $view->inputs->get('name', $data['name']); // 'old-name'
echo $view->inputs->get('more[key]'); // val
// and use it in form generator.
echo $view->forms->text('name', 'default');
// will output
```

The `DataView` object injects `Inputs` object into `Forms` helper so that it can generate HTML tags using old input values (i.e. *old-name*) instead of given value (*default*).

### List of Helpers

[](#list-of-helpers)

These helpers help to manage data to be viewed in a template. There are,

- `Escape` for escaping string,
- `Data` for managing data,
- `Inputs` for managing input-data,
- `Errors` for managing input-errors,
- `Message` for messages,
- `Forms` for generating form elements, and
- `Dates` for generating complex form elements.

Escape Helper
-------------

[](#escape-helper)

The `Escape` class manages the escaping string to securely display text in a certain content, such as HTML.

### Escaping for Html

[](#escaping-for-html)

As a default, strings will be escaped using `Escape::htmlSafe($string)` method (which internally uses `htmlspecialchars` function for HTML files). There are some other way to escape.

```
$esc = new Escape();
echo $esc('safe'); // or
echo $esc->escape('safe');
```

### Using Other Escape Method

[](#using-other-escape-method)

You can specify another escape method at the construction or using `withEscape` method:

```
$esc = new Escape('addslashes');
// or
$esc = $esc->setEscape('rawurlencode');
```

The escape method must be a callable; either a function name, or a closure with string as an argument.

### Using Escape with DataView

[](#using-escape-with-dataview)

The `DataView` object contains an `Escape` object to be shared with other helpers, such as `Data` and `Inputs`.

Specify the escape at the construction.

```
$view = new DataView(new Escape('addslashes'));
$view->inputs->get('with-slash'); // escaped with addslashes.
// or change how to escape
$view->escape->setEscape('rawurlencode');
```

Data Helper
-----------

[](#data-helper)

Use `Data` class to display strings and values to template while escaping the values.

```
// construct yourself.
$data = Data::forge(['view'=>'val'], $escape);

// or use DataView class.
$view = new DataView();
$view->setData(['some'=>'value']);
$data = $view->data;
```

to access data, any of the following works.

```
echo $data['view'];      // escaped
echo $data->view;        // escaped
echo $data->get('view'); // escaped
echo $data->raw('view'); // raw value
echo $data->get('none', 'non\'s'); // show escaped default value
```

### Iterating Over Array

[](#iterating-over-array)

the `Data` object implements IteratorAggregate interface.

```
$data1 = [
    'text' => 'testing',
    'more' => 'todo',
];
$data2 = [
    'text' => 'tested',
    'more' => 'done',
];
$data = Data::forge([$data1, $data2]);
foreach($data as $key => $val) {
    echo "$key: ", $val->text;
    echo "$key: ", $val->more; // escaped.
}
```

> only works for an array. so do not do this...
>
> ```
> $data = Data::forge([
>     'text' => 'tested',
>     'more' => 'done',
> ]);
> foreach($data as $key => $val) {
>     echo "$key: ", $val->get(null); // won't work!
> }
> ```

### Extract by Key

[](#extract-by-key)

Use `extractKey` method to create a subset of `Data` object if the data is an array of another array (or object).

```
$data = Data::forge([
    'obj'=>new ArrayObject['type' => 'object']
]);
$obj = $data->extractKey('obj');
echo $obj->type;  // object
```

### Hidden Tag

[](#hidden-tag)

There is a simple method to show a hidden tag:

```
$data = Data::forge(['_method'=>'put']);
echo $data->hiddenTag('_method');
//
```

Inputs Helper
-------------

[](#inputs-helper)

Use `Inputs` class to convenient access array of data using HTML form element names.

```
