Form model wanna to use global.php's config

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;
    }

To create a simple form factory that has access to global config:

class MyFormFactory
{
    public function __invoke(ContainerInterface $container)
    {
        $form = new MyForm();
        $form->setConfig($container->get('config'));
        return $form;
    }
}

With your init method, use the config varibles to create your custom form

class MyForm extends Form
{
    var $config;

    public function init()
    {
        $this->add([
            'name' => 'role', //角色,用字符串加深理解
            'type' => MultiCheckbox::class,
            'options' => [
                'value_options' => [
                    $this->config['roles']['name'] => $this->config['roles']['value'],
                    ...
                ],
            ],
        ]);
    }
}

Your config file should have this entry:

'form_elements' => [
    'factories' => [
        MyForm::class => MyFormFactory::class,
    ],
],

To get that form into your controller, you cannot do so by creating the object. From the controller factory, retrieve the FormElementManger Object:

...
$form = $container->get('FormElementManager')->get(MyForm::class);
$controller->setForm($form);
...

Thank you so much!! That’s what exactly I am looking for!!

I followed your advise and it do acquire the configuration of project_root/config/global.php
But!!!
When I changed the code to following, it changed on the html. It is no big deel, but cause a lot of trouble to me, because I use form Id as selector in my javascript code. I spent a lot of time to find out the problem. :joy:

module/Application/config/module.config.php
I added the following:

'form_elements' => [
        'factories' => [
            UserForm::class => UserFormFactory::class,
        ],
    ],

The userForm:

protected $roleArr = ['超级管理员' => '超级管理员', '编辑人员' => '编辑人员'];

    public function __construct($roleConfig)
    {
        $this->roleArr = $roleConfig;
        parent::__construct(null, []); //should I have to add this line???
    }

UserFormFactory:

public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null)
    {
        $config = $container->get('config');
        return new UserForm($config['roleConfig']);
    }