PHPackages                             web-complete/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. [Validation &amp; Sanitization](/categories/validation)
4. /
5. web-complete/form

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

web-complete/form
=================

Form model

1.1.0(8y ago)0854[1 issues](https://github.com/web-complete/form/issues)MITPHPPHP &gt;=7.0.0

Since Sep 17Pushed 8y ago1 watchersCompare

[ Source](https://github.com/web-complete/form)[ Packagist](https://packagist.org/packages/web-complete/form)[ RSS](/packages/web-complete-form/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (1)Versions (8)Used By (0)

Form model
==========

[](#form-model)

[![Build Status](https://camo.githubusercontent.com/64de36af3f067b5c86d7170beccdf28bdb7d9d5fbba02e80d0d826797aa37501/68747470733a2f2f7472617669732d63692e6f72672f7765622d636f6d706c6574652f666f726d2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/web-complete/form)[![Coverage Status](https://camo.githubusercontent.com/0bc5bcfefe9cfddf7e5eb34950410ca40d9cf5e1deb7ec8fcd88445b109150f5/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f7765622d636f6d706c6574652f666f726d2f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/web-complete/form?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/ba9683e17a4898aadfe81092b43744f885ba8512ead37f153ed8090b446c9b2e/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7765622d636f6d706c6574652f666f726d2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/web-complete/form/?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/c97120924cc86a426607651556d207aa8a2f62a6bcf8754c5e613204c63ec5d5/68747470733a2f2f706f7365722e707567782e6f72672f7765622d636f6d706c6574652f666f726d2f76657273696f6e)](https://packagist.org/packages/web-complete/form)[![License](https://camo.githubusercontent.com/70bf06d4765c413d210c64f7ce0acdecfbfa497399e0951ecf18fe1acf9ae488/68747470733a2f2f706f7365722e707567782e6f72672f7765622d636f6d706c6574652f666f726d2f6c6963656e7365)](https://packagist.org/packages/web-complete/form)

Flexible filtration and validation server side Form Model like in Yii2.

The library has no dependencies and can be easily used with any frameworks or code.

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

[](#installation)

```
composer require web-complete/form

```

Usage
-----

[](#usage)

To use the Form, you need to create a class which extend AbstractForm and implement abstract methods: rules () and filters (). In the primitive case, they can return empty arrays.

**Filters** (filtration rules) is an array of the following format:

```
[
    [field, filter, params]
]
```

where:

**field** - field name or field path (dot separated) or array of fields
**filter** - filter name. It can be a string or callable function. The Form will check method availability in the own object and if not exists then in filter objects array (filtersObject) in case of string. The filter will be called with arguments: $value, $params (parameters from the filtration rules), $form (current from object) and will return the filtered value.
**params** - array of parameters will be passed to the filter. The requirement depends on the called filter.

**Rules** (validation rules) is an array of the following format:

```
[
    [field, validator, params, message]
]
```

where:

**field** - field name or field path (dot separated) or array of fields
**validator** - validator name. It can be a string or callable function. It can be a string or callable function. The Form will check method availability in the own object and if not exists then in validator objects array (validatorsObject) in case of string. The validator will be called with arguments: $value and $params (parameters from the filtration rules) and will return boolean.
**params** - array of parameters will be passed to the filter. The requirement depends on the called filter.
**message** - Message in case of error. Returns "error" by default or value from overridden the $defaultError property.

If the data field does not have any filtering or validation rules, it will be deleted. However, if the field is necessary, but does not require filtering and validation, you can specify its security by adding it to the rules rules without additional arguments:

```
[
    ['name', 'string', ['min' => 3]],
    ['data.nested.field', 'string', ['min' => 3]],
    ['age'], // this field is considered as safe
]
```

The form has a built-in validator **required**, which checks that this field is not empty. The other validators will only be applied to non-empty fields.

The class constructor takes the following arguments:

```
    public function __construct(
        $rules = null,
        $filters = null,
        $validatorsObject = null,
        $filtersObject = null
    )
```

**rules** - will be merged with **rules()** (optional)
**filters** - will be merged with **filters()** (optional)
**validatorsObject** - object with validation methods (optional)
**filtersObject** - object with filtration methods (optional)

The form API provides the following methods:

**validate()** : - validate data
**setData($data)** - filter and set form data
**getData()** - get form data
**setValue($field, $value, $filter = true)** - filter (by default) and set form field value
**getValue($field)** - get form field value
**addError($field, $error)** - add an error for field
**hasErrors($field = null)** - check for errors in the form or field
**getErrors($field = null)** - get form or field errors
**getFirstErrors()** - get the first errors of all form fields
**resetErrors()** - reset form errors

This library has classes **Validators** and **Filters** which contains the most commonly used filters and validators and **FastForm** class form simple forms.

Filters
-------

[](#filters)

```
Filters supplied with the library (can be used independently):

```

**trim** - trim spaces (arguments: charlist, left, right)
**escape** - htmlspecialchars
**capitalize** - transform string to lowercase and capitalize first char
**lowercase** - transform string to lowercase
**uppercase** - transform string to uppercase
**replace** - replace substring (optional arguments: pattern (string or regular expression), to)
**stripTags** - strip html-tags
**stripJs** - strip js

For more information, see the **Filters** class annotations

Validators
----------

[](#validators)

```
Validators supplied with the library (can be used independently):

```

**equals** - comparison with other value (arguments: value, not (check inequality in case of **true**))
**compare** - comparison with other value (arguments: field (The third argument is the **Form** object), not (check inequality in case of **true**))
**email** - e-mail
**number** - validate if numeric (optional arguments: min, max)
**string** - validate if string (optional arguments: min, max - string length)
**regex** - regular expression (arguments: pattern)

For more information, see the **Validators** class annotations

Examples
--------

[](#examples)

Filtering and validation rules:

```
class MyForm1 extends \WebComplete\form\AbstractForm
{

    protected function filters()
    {
        return [
            [['first_name', 'last_name'], 'capitalize'],
            ['description', 'stripTags'],
            ['content', 'stripJs'],
            ['data.nested.field', 'trim'],
            ['email', 'replace', ['pattern' => 'email.com', 'to' => 'gmail.com']],
            ['*', 'trim'],
        ];
    }

    protected function rules()
    {
        return [
            [['description', 'label'], ], // safe fields (no validation)
            [['name', 'email'], 'required', [], 'Field is required'],
            ['name', 'string', ['min' => 2, 'max' => 50], 'Incorrect name'],
            ['email', 'email', [], 'Incorrect email'],
            ['price', 'validatePrice'],
            ['password', 'required'],
            ['password_repeat', 'compare', ['field' => 'password'], 'Repeat password error'],
            ['data.token', 'validateToken'],
            ['some', [SomeValidator::class, 'method'], ['customParam' => 100], 'Incorrect'],
            [['*'], 'regex', ['pattern' => '/^[a-z]$/'], 'Field is required'],
        ];
    }

    protected function validatePrice($value, $params, AbstractForm $form)
    {
        ...
        return true;
    }

    protected function validateToken($value, $params, AbstractForm $form)
    {
        ...
        return true;
    }

}
```

Form usage:

```
$form = new MyForm([], [], new Validators(), new Filters());
$form->setData($_POST);
if($form->validate()) {
    $filteredData = $form->getData();
    ...
}
```

Fast Form usage:

```
$form = new FastForm([['name', 'required'], ['email', 'email']]);
$form->setData($_POST);
if($form->validate()) {
    $filteredData = $form->getData();
    ...
}
else {
    $form->getErrors();
//    $form->getErrors('name');
//    $form->getFirstErrors();
//    $form->hasErrors();
    ...
}
```

Custom abstract form creation with default rules:

```
abstract class MyAbstractForm extends \WebComplete\form\AbstractForm
{

    protected function filters()
    {
        return [
            ['*', 'trim']
        ];
    }

    public function __construct($rules = [], $filters = [])
    {
        parent::__construct($rules, $filters, new Validators(), new Filters();
    }

}
```

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~25 days

Recently: every ~36 days

Total

7

Last Release

3011d ago

PHP version history (2 changes)1.0.0PHP &gt;=5.6.0

1.0.2PHP &gt;=7.0.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/e557e816560f9c9295e3d74205ce5ac83f84ac226ffe51de5375556acae3f9e9?d=identicon)[mvkasatkin](/maintainers/mvkasatkin)

---

Top Contributors

[![mvkasatkin](https://avatars.githubusercontent.com/u/3389061?v=4)](https://github.com/mvkasatkin "mvkasatkin (32 commits)")

---

Tags

modelyiiform

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/web-complete-form/health.svg)

```
[![Health](https://phpackages.com/badges/web-complete-form/health.svg)](https://phpackages.com/packages/web-complete-form)
```

###  Alternatives

[laminas/laminas-form

Validate and display simple and complex forms, casting forms to business objects and vice versa

8112.0M115](/packages/laminas-laminas-form)[siriusphp/validation

Data validation library. Validate arrays, array objects, domain models etc using a simple API. Easily add your own validators on top of the already dozens built-in validation rules

181743.3k13](/packages/siriusphp-validation)[apy/jsfv-bundle

Symfony2 Javascript Form Validation Bundle with localisation support

92770.5k](/packages/apy-jsfv-bundle)[yiibr/yii2-br-validator

Provide validations and features for brazilian localization

14103.2k4](/packages/yiibr-yii2-br-validator)[isometriks/spam-bundle

Provides spam protection for Symfony forms

46200.6k1](/packages/isometriks-spam-bundle)[progsmile/request-validator

Simple PHP Request Validator

33113.3k1](/packages/progsmile-request-validator)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
