PHPackages                             mvanvu/php-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. mvanvu/php-form

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

mvanvu/php-form
===============

Php Form package

1.0.2(4y ago)0761MITPHPPHP &gt;=7.1

Since Dec 8Pushed 4y ago1 watchersCompare

[ Source](https://github.com/mvanvu/php-form)[ Packagist](https://packagist.org/packages/mvanvu/php-form)[ Docs](https://github.com/mvanvu/php-form)[ RSS](/packages/mvanvu-php-form/feed)WikiDiscussions master Synced 1w ago

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

Php Form Package
================

[](#php-form-package)

Manage the form fields in easy way, security and cool.

Features
--------

[](#features)

- Render form via templates: Bootstrap (v3 and v4) and Uikit v3
- Ability to add new custom fields
- Ability to add new custom rules (for validation)
- Ability to translate the field
- Cool feature show/hide on
- Create once and using to render HTML form and validate from the PHP server

Included dependencies
---------------------

[](#included-dependencies)

- Php-registry (see )
- Php-filters (see )

Installation via Composer
-------------------------

[](#installation-via-composer)

```
{
	"require": {
		"mvanvu/php-form": "~1.0"
	}
}
```

Alternatively, from the command line:

```
composer require mvanvu/php-form
```

Testing
-------

[](#testing)

1 - Clone this repo:

`  git clone https://github.com/mvanvu/php-form.git    `

2 - Go to the repo

`cd php-from`

3 - Composer install

`composer install`

4 - Run test server

`php -S localhost:9000/tests`

5 - Open the browser with url localhost:9000/tests

Usage
-----

[](#usage)

```
use MaiVu\Php\Form\Form;

$fieldsData = [/*See tests/index.php to know how to create the fields data*/];
$form = new Form($fieldsData);

// Default template is Bootstrap (the both v3 and v4 are working)
echo $form->renderFields();

// Render Uikit 3 template
echo $form->renderTemplate('uikit-3');

// Or set default template
Form::setTemplate('uikit-3');
echo $form->renderFields();

// Render horizontal
echo $form->renderHorizontal();

// Validate form
if ($form->isValidRequest()) // The same $forms->isValid($_REQUEST)
{
    echo 'Cool! insert the valid data to the database';
    $data      = $form->getData(); // Instance of Registry
    $validData = $data->toArray();
    var_dump($validData);

}
else
{
    echo 'Oops. The form is invalid:' . implode('', $form->getMessages());
}
```

Forms manager
-------------

[](#forms-manager)

Using the forms manager to manage all your forms

```
use MaiVu\Php\Form\Form;
use MaiVu\Php\Form\FormsManager;

$fieldsData1 = [/*See tests/index.php to know how to create the fields data*/];
$fieldsData2 = [/*See tests/index.php to know how to create the fields data*/];
$form1 = new Form($fieldsData1);
$form2 = new Form($fieldsData2);
$forms = new FormsManager([$form1, $form2]);

// OR
// $forms = new FormsManager;
// $forms->add($form1)->add($form2);

echo $forms->renderFormFields(0);
echo $forms->renderFormFields(1);
// echo $forms->renderHorizontal(0);
// echo $forms->renderHorizontal(1);

// OR set name for the form
// $forms->set('form1', $form1);
// $forms->set('form2', $form2);
// echo $forms->renderFormFields('form1');

// Validate form
if ($forms->isValidRequest()) // The same $forms->isValid($_REQUEST)
{
    echo 'Cool! insert the valid data to the database';
    $validData = $forms->getData(true); // Get data as an array instead Registry
    var_dump($validData);
}
else
{
    echo 'Oops. The form is invalid:' . implode('', $forms->getMessages());
}
```

Consider using the form with name
---------------------------------

[](#consider-using-the-form-with-name)

```
use MaiVu\Php\Form\Form;
$fieldsData = [
    [
        'name'  => 'text',
        'type'  => 'text',
        'value' => null,
    ],
];
$form       = new Form('myForm', $fieldsData);
$form->bind(
    [
        'myForm' => [
            'text' => 'The text value',
        ],
    ]
);

echo $form->getField('text');
// Will render with the form name myForm[text]:

// Form deep name
$form = new Form('myForm.params', $fieldsData);
$form->bind(
    [
        'myForm' => [
            'params' => [
                'text' => 'The text value',
            ],
        ],
    ]
);

echo $form->getField('text');
//
```

Translate the field (for multilingual purpose, include php-form.min.css &amp; php-form.min.js)
----------------------------------------------------------------------------------------------

[](#translate-the-field-for-multilingual-purpose-include-php-formmincss--php-formminjs)

```
use MaiVu\Php\Form\Form;

// Set option languages
Form::setOptions(
    [
        'languages' => [
            // ISO code 2 => name
            'us' => 'en-US',
            'vn' => 'vi-VN',
        ]
    ]
);

// Then add/set translate = true for the field
$form = new Form(
    [
        [
            'name'      => 'hello',
            'type'      => 'Text',
            'label'     => 'Multilingual',
            'translate' => true,
        ],
   ]
);

// Bind with translations data
$form->bind(
    [
        'hello' => 'Hello world',
        'i18n'  => [
            'vi-VN' => [
                'hello' => 'Xin chào',
            ],
        ],
    ]
);

echo $form->renderFields(); // See tests/index.php

// By default all of translate fields are optional, no filters and no rules
// To enable them

$form = new Form(
    [
        [
            'name'      => 'hello',
            'type'      => 'Text',
            'label'     => 'Multilingual',
            'translate' => [
                'required' => true,
                'filters'  => ['string', 'trime'],
                'rules'    => [
                    'Confirm:abc123' => 'The multilingual must be: abc123',
                ],
            ],
        ],
   ]
);
```

Default fields see at path src/Field
------------------------------------

[](#default-fields-see-at-path-srcfield)

- Switcher (must include assets/css/php-form.min.css if you don't use the php-assets)
- Check
- CheckList
- Email
- Hidden
- Number
- Password
- Radio
- Select
- SubForm
- Text
- TextArea

SubForm field
-------------

[](#subform-field)

Display a group fields width in the grid columns layout

```
    use MaiVu\Php\Form\Form;

    $form = new Form(
        [
            [
                'name'        => 'subform',
            	'type'        => 'SubForm',
                'label'       => 'Subform',
                'description' => 'This is a subform',
                'columns'     => 2,
                'horizontal'  => false,
                'fields'      => [
                    [
            		    'name'  => 'firstName',
            		    'type'  => 'Text',
            		    'class' => 'form-control',
            		    'hint'  => 'First name',
            	    ],
            	    [
            		    'name'  => 'lastName',
            		    'type'  => 'Text',
            		    'class' => 'form-control',
            		    'hint'  => 'Last name',
                    ],
                ]
           ],
       ]
    );
```

Show on feature
---------------

[](#show-on-feature)

Show or hide the base field in the conditions (UI likes the Joomla! CMS Form)

```
    use MaiVu\Php\Form\Form;

    $form = new Form(
        [
            [
                'name'        => 'pass1',
                'type'        => 'Password',
                'label'       => 'Password',
                'class'       => 'form-control',
                'description' => 'Enter the password min length >= 4 to show the confirm pass word',
                'required'    => true,
            ],
            [
                'name'     => 'pass2',
                'type'     => 'Password',
                'label'    => 'Confirm password',
                'class'    => 'form-control',
                'required' => true,
                'showOn'   => 'pass1:! & pass1:>=4',
                'rules'    => [
                	'Confirm:pass1'                 => 'Password is not match!',
                	'Confirm:pass1|2468'            => 'Password must be: 2468',
                	'Confirm:pass1|4567[when:1234]' => 'Please, when this is 1234 then the Password must be: 4567',
                ],
            ],
        ]
    );

    // Before render field we must include assets/js/php-form.min.js
    echo $form->renderFields();
```

Show on values
--------------

[](#show-on-values)

### Format: {fieldName}:{markup}

[](#format-fieldnamemarkup)

- {fieldName} = the name of field
- {markup} = the format of {fieldName} value

#### For eg: the {fieldName} = MyField

[](#for-eg-the-fieldname--myfield)

Show when MyField is empty `showOn => 'MyField:'`

Show when MyField is not empty `showOn => 'MyField:!'`

Show when MyField min length is 5 `showOn => 'MyField:>=5'`

Show when MyField max length is 15 `showOn => 'MyField:=4',
        'rules'    => [
            'Confirm:pass1'                 => 'Password is not match!',
            'Confirm:pass1|2468'            => 'Password must be: 2468',
            'Confirm:pass1|4567[when:1234]' => 'Please, when this is 1234 then the Password must be: 4567',
        ],
    ];

```

### Email

[](#email)

```
    // Just use the Email type
    $email = [
        'name'     => 'Email',
        'type'     => 'Email',
        'label'    => 'My Email',
        'messages' => [
            'Email' => 'Invalid email.'
        ],
    ];

    // OR set its rules contain Email: 'rules' => ['Email']
```

### Date

[](#date)

Check the value is a valid date

### MinLength and MaxLength

[](#minlength-and-maxlength)

```
    $text = [
        'name'     => 'MyField',
        'type'     => 'TextArea',
        'label'    => 'My Field',
        'rules'    => ['MinLength:5', 'MaxLength:15'],
        'messages' => [
            'MinLength:5'  => 'Minimum is 5 chars.',
            'MaxLength:15' => 'Maximum is 15 chars.'
        ],
    ];
```

### Options

[](#options)

```
    // Invalid if the value is not in the options attributes
    $select = [
        'name'     => 'MyField',
        'type'     => 'Select',
        'label'    => 'My Field',
        'options'  => [
            [
                'value' => '1',
                'text'  => 'Yes',
            ],
            [
                'value' => '0',
                'text'  => 'No',
            ],
        ],
        'rules'    => ['Options'],
        'messages' => [
            'Options' => 'The value not found.', // Invalid if the value is not (1 or 0)
        ],
    ];
```

### Regex

[](#regex)

```
    $regex = [
        'name'     => 'MyField',
        'type'     => 'TextArea',
        'label'    => 'My Field',
        'rules'    => [
            'Regex:^[0-9]+$' => 'The value must be an unsigned number',
        ],
    ];
```

### Custom function

[](#custom-function)

```
    $switcher = [
        'name'     => 'MyField',
        'type'     => 'Switcher',
        'label'    => 'My Field',
        'rules'    => [
            'custom' => function ($field) {
                $isValid = $field->isChecked();

                if (!$isValid)
                {
                    $field->setMessage('custom', 'Please enable this field');
                }

                return $isValid;
            },
        ],
    ];
```

Extends Field and Rule
----------------------

[](#extends-field-and-rule)

Create all your fields at src/Field, the field must be extended \\MaiVu\\Php\\Form\\Field class

AND

Create all your rules at src/Rule, the rule must be extended \\MaiVu\\Php\\Form\\Rule class

OR

if you want to use your custom namespace

```

    use MaiVu\Php\Form\Form;

    Form::addFieldNamespaces('Your\\Custom\\MyNS');
    Form::addRuleNamespaces('Your\\Custom\\MyNS');
```

Then create your FieldClass in your namespace

```
   namespace Your\Custom\MyNS;
   use MaiVu\Php\Form\Field;

   class MyCustomField extends Field
   {
        public function toString()
        {
            return 'Hello World!'; // Return input field
        }
   }

   // Usage: type => 'MyCustomField'

```

Create your RuleClass in your namespace

```
   namespace Your\Custom\MyNS;
   use MaiVu\Php\Form\Rule;
   use MaiVu\Php\Form\Field;

   class MyCustomRule extends Rule
   {
        // Php validator
        public function validate(Field $field) : bool
        {
            return $field->getValue() === '1'; // Value = 1 is valid or not
        }
   }

   // Usage: rules => ['MyCustomRule']

```

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 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 ~85 days

Total

3

Last Release

1818d ago

### Community

Maintainers

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

---

Top Contributors

[![mvanvu](https://avatars.githubusercontent.com/u/5796168?v=4)](https://github.com/mvanvu "mvanvu (77 commits)")

---

Tags

phpregistry

### Embed Badge

![Health badge](/badges/mvanvu-php-form/health.svg)

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

PHPackages © 2026

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