https://github.com/yiisoft/form-model
https://github.com/yiisoft/form-model
form forms yii3
Last synced: 5 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/yiisoft/form-model
- Owner: yiisoft
- License: bsd-3-clause
- Created: 2023-12-01T13:00:19.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-04-04T11:38:55.000Z (over 1 year ago)
- Last Synced: 2024-04-14T00:22:19.740Z (about 1 year ago)
- Topics: form, forms, yii3
- Language: PHP
- Homepage:
- Size: 79.1 KB
- Stars: 3
- Watchers: 15
- Forks: 1
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
Yii Form Model
[](https://packagist.org/packages/yiisoft/form-model)
[](https://packagist.org/packages/yiisoft/form-model)
[](https://github.com/yiisoft/form-model/actions/workflows/build.yml)
[](https://codecov.io/gh/yiisoft/form-model)
[](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/form-model/master)
[](https://github.com/yiisoft/form-model/actions?query=workflow%3A%22static+analysis%22)
[](https://shepherd.dev/github/yiisoft/form-model)
[](https://shepherd.dev/github/yiisoft/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](https://getcomposer.org):
```shell
composer require yiisoft/form-model
```## General usage
Define a [form model](docs/guide/en/form-model.md):
```php
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](docs/guide/en/form-hydrator.md):
```php
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](docs/guide/en/displaying-fields.md) in the view:
```php
use Yiisoft\FormModel\Field;
use Yiisoft\FormModel\FormModel;echo Field::text($formModel, 'login');
echo Field::password($formModel, 'password');
echo Field::checkbox($formModel, 'rememberMe');// ...
```## Documentation
- [Guide](docs/guide/en/README.md)
- [Internals](docs/internals.md)If you need help or have a question, the [Yii Forum](https://forum.yiiframework.com/c/yii-3-0/63) is a good place for
that. You may also check out other [Yii Community Resources](https://www.yiiframework.com/community).## License
The Yii Form Model is free software. It is released under the terms of the BSD License.
Please see [`LICENSE`](./LICENSE.md) for more information.Maintained by [Yii Software](https://www.yiiframework.com/).
## Support the project
[](https://opencollective.com/yiisoft)
## Follow updates
[](https://www.yiiframework.com/)
[](https://twitter.com/yiiframework)
[](https://t.me/yii3en)
[](https://www.facebook.com/groups/yiitalk)
[](https://yiiframework.com/go/slack)