jin2chen / form-model
The package helps with implementing data entry forms.
Installs: 3
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 1
pkg:composer/jin2chen/form-model
Requires
- php: ^7.4|^8.0
- yiisoft/validator: 3.0.x-dev
Requires (Dev)
- phpunit/phpunit: ^9.5
- vimeo/psalm: ^4.7
This package is auto-updated.
Last update: 2026-02-10 17:32:03 UTC
README
The package helps with implementing data entry forms.
Installation
The package could be installed via composer:
composer require jin2chen/form-model --prefer-dist
Usage
You must create your form model by extending the abstract form class, defining all the private properties with their respective typehint.
Example: LoginForm.php
<?php
declare(strict_types=1);
namespace App\Form;
use jin2chen\FormModel\FormModel;
use Yiisoft\Validator\Rule\Email;
use Yiisoft\Validator\Rule\Required;
use Yiisoft\Validator\Rule\HasLength;
use Yiisoft\Validator\Validator;
class LoginForm extends FormModel
{
public string $username = '';
public string $password = '';
public bool $rememberMe = false;
/** Add rules */
public function getRules(): array
{
return [
'login' => $this->loginRules()
];
}
private function loginRules(): array
{
return [
Required::rule(),
HasLength::rule()
->min(4)
->max(40)
->tooShortMessage('Is too short.')
->tooLongMessage('Is too long.'),
Email::rule()
];
}
}
$form = new LoginForm();
$validator = new Validator();
$results = $validator->validate($form);
$results->isValid();
Unit testing
The package is tested with PHPUnit. To run tests:
./vendor/bin/phpunit
Static analysis
The code is statically analyzed with Psalm. To run static analysis:
./vendor/bin/psalm