yiisoft / form-model
Provides a base for form models and helps to fill, validate and display them.
Fund package maintenance!
Opencollective
yiisoft
Installs: 14 265
Dependents: 1
Suggesters: 0
Security: 0
Stars: 13
Watchers: 17
Forks: 4
Open Issues: 4
Requires
- php: ^8.1
- ext-mbstring: *
- psr/http-message: ^1.0|^2.0
- yiisoft/form: ^1.0
- yiisoft/html: ^3.3
- yiisoft/hydrator: ^1.3
- yiisoft/strings: ^2.3
- yiisoft/validator: ^2.1
Requires (Dev)
- httpsoft/http-message: ^1.1
- maglnet/composer-require-checker: ^4.7
- phpunit/phpunit: ^10.5
- rector/rector: ^1.2
- roave/infection-static-analysis-plugin: ^1.34
- spatie/phpunit-watcher: ^1.23
- vimeo/psalm: ^5.25
- yiisoft/di: ^1.2
This package is auto-updated.
Last update: 2024-11-09 14:29:51 UTC
README
Yii Form Model
The package provides a base for form models and helps to fill them with data, validate them and display them.
Requirements
- PHP 8.1 or higher.
mbstring
PHP extension.
Installation
The package could be installed with Composer:
composer require yiisoft/form-model
General usage
Define a form model:
use Yiisoft\FormModel\Attribute\Safe; use Yiisoft\FormModel\FormModel; use Yiisoft\Validator\Rule\Email; use Yiisoft\Validator\Rule\Length; use Yiisoft\Validator\Rule\Required; final class LoginForm extends FormModel { #[Label('Your login')] #[Required] #[Length(min: 4, max: 40, skipOnEmpty: true)] #[Email(skipOnEmpty: true)] private ?string $login = null; #[Label('Your password')] #[Required] #[Length(min: 8, skipOnEmpty: true)] private ?string $password = null; #[Label('Remember me for 1 week')] #[Safe] private bool $rememberMe = false; }
Fill it with data and validate using form hydrator:
use Psr\Http\Message\RequestInterface; use Yiisoft\FormModel\FormHydrator; use Yiisoft\FormModel\FormModel; final class AuthController { public function login(RequestInterface $request, FormHydrator $formHydrator): ResponseInterface { $formModel = new LoginForm(); $errors = []; if ($formHydrator->populateFromPostAndValidate($formModel, $request)) { $errors = $formModel->getValidationResult()->getErrorMessagesIndexedByProperty(); } // You can pass $formModel and $errors to the view now. } }
Display it using fields in the view:
use Yiisoft\FormModel\Field; use Yiisoft\FormModel\FormModel; echo Field::text($formModel, 'login'); echo Field::password($formModel, 'password'); echo Field::checkbox($formModel, 'rememberMe'); // ...
Documentation
If you need help or have a question, the Yii Forum is a good place for that. You may also check out other Yii Community Resources.
License
The Yii Form Model is free software. It is released under the terms of the BSD License.
Please see LICENSE
for more information.
Maintained by Yii Software.