Yii Form Model 1.0
2024-08-27
531次浏览
First release of Yii Form Model package was done. It provides a base for form models and helps to fill, validate and display them.
For 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');
// ...
新闻归档
热门标签
- yii3
- 扩展
- 发布
- yii2
- debug
- auth client
- view
- bootstrap
- release
- html
- redis
- apidoc
- Yii 2.0
- Yii 1.1
- mongodb
- mailer
- extensions
- twig
- gii
- queue
- runner
- http 客户端
- console
- validation
- cache
- sphinx
- widget
- hydrator
- translator
- symfonymailer
- db
- http
- log
- error handler
- config
- swiftmailer
- 图书
- imagine
- assets
- i18n
- IDE
- swagger
- elasticsearch
- router
- csrf
- smarty
- definitions
- logging
- fastroute
- user