PHPackages                             pframework/p-input - 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. pframework/p-input

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

pframework/p-input
==================

A architecture for filtering and validating user input using callables and arrays

0.1.0(12y ago)07.6k[1 issues](https://github.com/pframework/p-input/issues)UNLICENSEPHP

Since Sep 11Pushed 12y agoCompare

[ Source](https://github.com/pframework/p-input)[ Packagist](https://packagist.org/packages/pframework/p-input)[ RSS](/packages/pframework-p-input/feed)WikiDiscussions master Synced 5d ago

READMEChangelogDependenciesVersions (2)Used By (0)

P\_InputSet
===========

[](#p_inputset)

P\_InputSet is a small component that handles sets of input. For each particular piece of input an array defined to handle how the input is mapped, filtered/validated, if it is required to be in the set, and what a particular error message might be if it is not valid input.

A full array might look like this:

```
$config = [
    'first-name' => [
        'name' => 'first_name', // mapped name
        'required' => true,
        'process' => function ($value, $source) {
            if (strlen($value) < 2) { // validation
                return false;
            }
            return $value; // could potentially filter before returning
        },
        'error' => 'First name must be more than 2 characters'
    ]
];
$is = new P\InputSet($config);
$result = $is->process($_POST); // use post in this case
$result->isValid();
echo $result['first_name'];
```

Examples
========

[](#examples)

This is a 2 part example, a controller and the corresponding form in html. Note that the InputSet component does not attempt to do any kind of escaping of output, that would be the job of any particular view layer you employ the use of.

```
class FormController {
    public function handle() {
        if ($_POST) {
            $inputResult = $this->getUserInput($_POST);
            if ($inputResult->isValid()) {
                var_dump($inputResult);
                exit;
            }
        }
        include 'form.phtml';
    }

    protected function getUserInput($source) {
        $p = new P\InputSet([
            'username' => [
                'required' => true,
                'process' => function ($value, $source) {
                    if (!preg_match('#^[a-zA-Z0-9_]*$#', $value) || strlen($value)  'Username must be at least 5 characters and be only numbers and letters'
            ],
            'password' => [
                'required' => true,
                'error' => 'Password is required'
            ]
        ]);
        return $p->process($source);
    }
}

(new FormController)->handle();
```

And the form:

```
