Skip to content

Problems with replacing Form:getInstance #46510

@prinswebdevelopment

Description

@prinswebdevelopment

Joomla 5.4 (but I see the same problem in 6.1):

Form:getInstance (libraries/src/Form/Form.php:1691) is deprecated. The replacement doesn't work the same way, however, because the old method uses a singleton pattern. If you currently use Model:preprocessForm in your model and want to edit a subform, it will no longer work. In the 'old' method, you can do this:

protected function preprocessForm(Form $form, $data, $group = 'content')
    {
        parent::preprocessForm($form, $data, $group);

        $subFormField = $form->getField('subform');

        $i = 0;
        foreach ($data->surveys as $key => $row) {
        
            $subForm = Form::getInstance('subform.surveys' . $i, $subFormField->__get('formsource'), ['control' => 'jform[surveys][survey' . $i . ']']);

			// create new field
			$field = new \SimpleXMLElement('<field></field>');
			$field->addAttribute('name', 'readonly_spacer');
            $field->addAttribute('type', 'spacer');
            $field->addAttribute('hiddenLabel', 'true');
            $field->addAttribute('description', Text::_('TEXT'));
            
            // Voeg het veld toe aan het formulier
            $subForm->setField($field);

        }
        $i++;
    }
}

But in the new method:

$subForm = Form::getInstance('subform.surveys' . $i, $subFormField->__get('formsource'), ['control' => 'jform[surveys][survey' . $i . ']']);

replaced:

$formFactory = Factory::getContainer()->get(FormFactoryInterface::class);
$subForm = $formFactory->createForm('subform.surveys' . $i, ['control' => 'jform[surveys][survey' . $i . ']']);
$subForm->load($subFormField->__get('formsource'));

This doesn't work because if the form is loaded in libraries/src/Form/Field/SubformField.php:385, a new object is created and the old one isn't reused. Furthermore, I believe it's impossible to edit a subform in the model.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions