Hi there,
I have read up on tabular data and forms with 2 or more models, but they don’t quite fit for what I’m trying to accomplish, or if they do, I’m not quite getting it.
I have a model widget
with fields widget_id, widget_name, widget_type_id widget_config_data… ect.
widget_id and widget_name are self explanatory
widget_type_id references the model widget_type
model widgettype has the fields widget_type_id, label, modelname
widget_config_data stores a serialized array
There are 4 records in model widgettype
1, widget a, Widgeta
2, widget b, Widgetb
ect…
In \app\models\ I have Widget.php, Widgettype.php, Widgeta.php Widgetb.php etc…
Which define the fields necessary for the specific widgettype to function. i.e.
Widgeta defines the fields connection_string, username, password
Widgetb defines the fields source_url, local_filepath, remote_filepath
So, in my widget form, instead of the textfield “widget_config_data”
I want use the widgettype’s reference to the corresponding model to display a subform built off the definitions of the widget type
i.e.
if widget_id 1 has widgettype = Widgeta
I want a form that shows the fields defined in the model Widgeta
connection_string, username, password,
and for that form populated by the values in the serialized array stored in the field widget.widget_config_data.
My plan was to have the model $widget->widget_config_data field be hidden and populated via jquery with the serialized values of the subform generated by model Widgeta
Any guidance would be much appreciated.
Clear as mud?
Here’s where I’m at so far:
\app\controllers\WidgetController.php
...
use app\models\Widget;
use app\models\WidgetTypeAConfigForm;
....
public function actionCreate()
{
$model = new Widget();
$widgettypea = new WidgetTypeAConfigForm();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->import_channels_id]);
} else {
return $this->render('create', [
'model' => $model,
'widgettypea' => $widgettypea,
]);
}
}
\views\widget\_form.php
...
use app\models\Widgettype;
use app\models\WidgettypeaConfigForm;
...
<?php
foreach($widgettype as $wt){
?>
<div id="widget_type_<?= $wt->widget_type_id ?>">
<?= $this->render('@app/views/'.strtolower($wt->method).'configform.php') ?> // for this example rendering file /views/widgettypeaconfigform.php
</div>
<?php
}
?>
/views/widgettypeaconfigform.php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $widgettypea \app\models\WidgettypeaConfigForm */
/* @var $form ActiveForm */
?>
<div class="widgettypeaconfigform">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($widgettypea, 'field1') ?>
<?= $form->field($widgettypea, 'field2') ?>
<?= $form->field($widgettypea, 'field3') ?>
<?= $form->field($widgettypea, 'field4') ?>
<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
\app\models\WidgettypeaConfigForm.php
<?php
namespace app\models;
use Yii;
use yii\base\Model;
class WidgettypeaConfigForm extends Model
{
public $field1;
public $field2;
public $field3;
public $field4;
public function rules()
{
return [
[['field1', 'field2', 'field3', 'field4'], 'required'],
];
}
}
Getting Error Message:
Undefined variable: widgettypea
in /views/widgettypeaconfigform.php
line: <?= $form->field($widgettypea, 'field1') ?>