I wanna to use some global configuration in a form model. should I use the DI method to resolve this?
I can not find any demo about the form-factory kind.
My form as follow, the role should be a configuration from global.php:
<?php
/**
* user 用户表
*/
namespace Application\Form;
use Laminas\Filter\StringTrim;
use Laminas\Filter\StripTags;
use Laminas\Filter\ToInt;
use Laminas\Form\Element\Hidden;
use Laminas\Form\Element\MultiCheckbox;
use Laminas\Form\Element\Password;
use Laminas\Form\Element\Text;
use Laminas\Form\Form;
use Laminas\InputFilter\InputFilterProviderInterface;
use Laminas\Validator\EmailAddress;
use Laminas\Validator\NotEmpty;
use Laminas\Validator\Regex;
class UserForm extends Form implements InputFilterProviderInterface
{
public function init() : void
{
$this->add([
'name' => 'id',
'type' => Hidden::class,
]);
$this->add([
'name' => 'username', //用户名,唯一
'type' => Text::class,
'attributes' => [
'placeholder' => '输入用户名',
],
]);
$this->add([
'name' => 'realname', //真实姓名
'type' => Text::class,
]);
$this->add([
'name' => 'passwd', //加点盐之后的sha1
'type' => Password::class,
'attributes' => [
'placeholder' => '输入密码',
],
]);
$this->add([
'name' => 'repasswd', //确认密码
'type' => Password::class,
'attributes' => [
'placeholder' => '再输入一遍密码',
],
]);
$this->add([
'name' => 'mobile', //手机号
'type' => Text::class,
]);
$this->add([
'name' => 'email', //电子邮箱
'type' => Text::class,
]);
$this->add([
'name' => 'role', //角色,用字符串加深理解
'type' => MultiCheckbox::class,
'options' => [
'value_options' => [
'超级管理员' => '超级管理员',
'编辑人员' => '编辑人员',
],
],
]);
$this->add([
'name' => 'valid', //0无效,1有效
'type' => Hidden::class,
]);
$this->add([
'name' => 'addtime', //添加时间戳
'type' => Hidden::class,
]);
$this->add([
'name' => 'uptime', //更新时间
'type' => Hidden::class,
]);
$this->add([
'name' => 'formhash', //rotate验证
'type' => Hidden::class,
]);
}
public function getInputFilterSpecification() : array
{
return [
[
'name' => 'id',
'required' => true,
'filters' => [
['name' => ToInt::class],
],
'validators' => [
[
'name' => NotEmpty::class,
'options' => ['message' => 'id不能为空'],
],
],
],
[
'name' => 'username', //用户名,唯一
'required' => true,
'filters' => [
['name' => StripTags::class],
['name' => StringTrim::class],
],
'validators' => [
[
'name' => NotEmpty::class,
'options' => ['message' => 'username不能为空'],
],
],
],
[
'name' => 'realname', //真实姓名
'required' => true,
'filters' => [
['name' => ToInt::class],
],
'validators' => [
[
'name' => NotEmpty::class,
'options' => ['message' => 'realname不能为空'],
],
],
],
[
'name' => 'passwd', //加点盐之后的sha1
'required' => true,
'filters' => [
['name' => StripTags::class],
['name' => StringTrim::class],
],
'validators' => [
[
'name' => NotEmpty::class,
'options' => ['message' => 'passwd不能为空'],
],
],
],
[
'name' => 'repasswd', //再次输入密码
'required' => true,
'filters' => [
['name' => StripTags::class],
['name' => StringTrim::class],
],
'validators' => [
[
'name' => NotEmpty::class,
'options' => ['message' => 'passwd不能为空'],
],
],
],
[
'name' => 'mobile', //手机号
'required' => true,
'filters' => [
['name' => StripTags::class],
['name' => StringTrim::class],
],
'validators' => [
[
'name' => NotEmpty::class,
'options' => ['message' => 'mobile不能为空'],
],
[
'name' => Regex::class,
'options' => [
'pattern' => '/^1(3|4|5|6|7|8|9)\d{9,9}$/',
'message' => '手机号格式不正确',
],
],
],
],
[
'name' => 'email', //电子邮箱
'required' => true,
'filters' => [
['name' => StripTags::class],
['name' => StringTrim::class],
],
'validators' => [
[
'name' => NotEmpty::class,
'options' => ['message' => 'email不能为空'],
],
[
'name' => EmailAddress::class,
'options' => ['message' => 'email不正确'],
],
],
],
[
'name' => 'salt', //加点盐
'required' => true,
'filters' => [
['name' => ToInt::class],
],
'validators' => [
[
'name' => NotEmpty::class,
'options' => ['message' => 'salt不能为空'],
],
],
],
[
'name' => 'role', //角色,用字符串加深理解
'required' => true,
'filters' => [
['name' => StripTags::class],
['name' => StringTrim::class],
],
'validators' => [
[
'name' => NotEmpty::class,
'options' => ['message' => 'role不能为空'],
],
],
],
[
'name' => 'valid', //0无效,1有效
'required' => true,
'filters' => [
['name' => ToInt::class],
],
'validators' => [
[
'name' => NotEmpty::class,
'options' => ['message' => 'valid不能为空'],
],
],
],
[
'name' => 'addtime', //添加时间戳
'required' => true,
'filters' => [
['name' => ToInt::class],
],
'validators' => [
[
'name' => NotEmpty::class,
'options' => ['message' => 'addtime不能为空'],
],
],
],
[
'name' => 'uptime', //更新时间
'required' => true,
'filters' => [
['name' => ToInt::class],
],
'validators' => [
[
'name' => NotEmpty::class,
'options' => ['message' => 'uptime不能为空'],
],
],
],
];
}
}
My controller’s action as follow:
class UserController extends AbstractActionController
{
public $baseFuncModel;
public $formElementManager;
public $userTable;
/** 依赖注入 */
public function __construct(BaseFuncModel $baseFuncModel, FormElementManager $formElementManager, UserTable $userTable)
{
$this->baseFuncModel = $baseFuncModel;
$this->formElementManager = $formElementManager;
$this->userTable = $userTable;
}
public function addAction()
{
/** @var UserForm $form */
$form = $this->formElementManager->get(UserForm::class);
$request = $this->getRequest();
if (!$request->isPost())
{
$vm = new ViewModel(['form' => $form]);
$vm->setTerminal(true);
return $vm;
}
$form->setValidationGroup('username','mobile','email','role','passwd','repasswd');
$form->setData($request->getPost());
if (!$form->isValid())
{
$vm = new ViewModel(['form' => $form]);
$vm->setTerminal(true);
return $vm;
}
$user = new User();
$user->exchangeArray($form->getData());
$result = $this->userTable->save($user);
//保存数据
$vm = new ViewModel();
$vm->setTerminal(true);
return $vm;
}