I am currently trying to make a widget for my project that would make subforms that can be cloned (inspired by multimodelform extension for Yii 1.1).
The tricky issue I ran into is that internally the widget needs an ActiveForm instance to create fields.
As it is not possible to have a nested form, I can’t just make a new ActiveForm inside the widget.
Currently I am passing the main form as a parameter to the widget, which then uses it for fields:
/* View file */ $form = ActiveForm::begin() // Main form stuff $form->field(...) MultiModelWidget::widget(['parentForm' => $form, ...]) // Pass config ActiveForm::end() /* MultiModelWidget class */ function renderField(...) ... return $this->parentForm->field(...);
However, I am not sure I like this solution. It seems that it adds unneeded coupling between the form and the widget.
Does anyone have some suggestions on handling this? Or maybe I’m wrong and this solution is fine?