ZF2,控制器不会从附加到表单的水合对象中填充表单元素

ZF2, controller doesn't fill the form elements from hydrated objects that are attached to the form

在 ZF2 中,我有一个包含两个字段集的表单。这 2 个字段集基本上用于与派对相关的信息(派对 = 个人或公司)和 phone 相关信息。使用像这样的形式的 init 方法调用字段集

class PhoneRegistrationForm extends RegistrationForm implements InputFilterAwareInterface
{
    public function __construct()
    {
        parent::__construct();

        $this
            ->setAttribute('method', 'post')
            ->setHydrator(new ClassMethods(false))
            ->setObject(new Phone());

        $this->add([
            'type' => 'Zend\Form\Element\Button',
            'name' => 'submitButton',
            'attributes' => [
                'id' => 'submit-phone-button',
                'class' => 'btn2 submit-button',
                'type' => 'submit',
            ],
            'options' => [
                'label' => 'Сохранить',
            ],
        ]);
    }

    public function init()
    {
        $this->add([
            'type' => 'Parties\Forms\Fieldsets\PhoneFieldset',
            'name' => 'phoneFieldset'
        ]);

        $this->add([
            'type' => 'Parties\Forms\Fieldsets\PartyIdAndRolesFieldset',
            'name' => 'partyFieldset'
        ]);
    }
}

在控制器的编辑操作中,我使用数据库中的值创建并混合了 PhoneObject 和 PartyObject(PhoneObject 和 PartyObjects 只是 getter 和 setter 的集合)。然后我将水合对象设置为字段集:

$this->form->get('phoneFieldset')->setObject($phone);
$this->form->get('partyFieldset')->setObject($party);

这是我的问题,如果我 return 控制器的表单实例,表单输入没有来自附加水合对象的值。

如何使用附加到字段集的水化对象的值填充表单输入?

我尝试绑定 $this->form->bind($phone),但无济于事。 fieldsets 也有 ClassMethods() hydrator 设置。

可能是什么问题?有什么线索吗?

编辑:@hooli 回答后,这是我使用的字段集的代码。

class PhoneFieldsetFactory extends Fieldset implements InputFilterProviderInterface
{
    public function __invoke(FormElementManager $formElementManager)
    {
        $this
            ->setHydrator(new ClassMethods())
            ->setObject(new Phone());

        $this->add([
            'type' => 'hidden',
            'name' => 'id',
        ]);

        $this->add(
            $formElementManager
                ->get('Parties\Forms\Elements\ContactMechanismTypeSelect',
                    ['mechanismType' => 'телефон'])
        );

        $this->add(
            $formElementManager
                ->get('Parties\Forms\Elements\CountrySelect')
        );

        $this->add(
            $formElementManager
                ->get('Parties\Forms\Elements\ContactMechanismPurposeTypeMultiCheckbox',
                    ['mechanismType' => 'телефон'])
        );

        $this->add([
            'type' => 'text',
            'name' => 'areaCode',
            'attributes' => [
                'id' => 'area-code',
                'placeholder' => '1234',
            ],
            'options' => [
                'label' => 'Код области'
            ],
        ]);

        $this->add([
            'type' => 'text',
            'name' => 'phoneNbr',
            'attributes' => [
                'id' => 'phone-nbr',
                'placeholder' => '1234567890',
            ],
            'options' => [
                'label' => 'Номер телефона',
            ],
        ]);

        $this->add([
            'type' => 'text',
            'name' => 'extension',
            'attributes' => [
                'id' => 'extension',
                'placeholder' => '1234567890',
            ],
            'options' => [
                'label' => 'Добавочный',
            ],
        ]);

        $this->add([
            'type' => 'text',
            'name' => 'comment',
            'attributes' => [
                'id' => 'comment',
                'placeholder' => 'общий телефон организации',
            ],
            'options' => [
                'label' => 'Примечание',
            ],
        ]);

        return $this;
    }
}

这不是一个真正好的解决方案,请随意提出更好的变体!如果您的回答更好,我会采纳。

经过一天的试验和错误仍然无法弄清楚为什么表单输入没有根据附加到字段集的对象来填充。不过,我找到了一个解决方法。

ZF2 Fieldset 实现方法 populateValues()。如果你使用这样的东西

$this->form->get('neededFieldset')->populateValues(
    $this->form->getHydrator()->extract($objectWhoseValuesWillBeExtracted));

您将用相应对象的数据填充表单的输入。

以下是我对您的问题的理解:

当你声明这个时 :

$this
        ->setAttribute('method', 'post')
        ->setHydrator(new ClassMethods(false))
        ->setObject(new Phone());

当您使用 bind 方法时,PhoneRegistrationForm 对象将填充您的 Phone 对象,但您没有 Phone 对象的任何字段在直接 PhoneRegistrationForm 中,因为您使用了字段集。

那么您应该更新您的字段集以设置正确的水化器策略。

此外,对于您的其他字段集也是相同的解决方案。

我平时用的是学说和实体,其实是一样的道理