How to pass $form as part of Tab onClick?

I’m trying to use a tab’s onClick event to dynamically load a view. This way it only loads when actually needed.

So the parent view tab is defined as

                [
                    'label' => 'Rates',
                    'linkOptions' => [
                        'id' => 'tabRates', 
                        'onclick' => 'loadRates(' . $model->EmployeeId . ');',
                    ],
                ],

Then the loadRates uses Ajax to run a controller action which is

    public function actionListEmployeeRates($id)
    {
        if(is_numeric($id)){
            $model = $this->findModel($id);

            $modelRates = EmployeesRates::find()
                ->where(['EmployeeId' => $id])
                ->orderBy(['[EffectiveAsOf]' => SORT_DESC])
                ->all();

            return $this->renderAjax('_form_escort_rates', 
                [
                    'model' => $model,
                    'modelRates' => $modelRates,
                ]
            );
        }

        throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
    }

and finally the actual rendered view includes

echo $form->field(...)

I have 2 issues that I’m hoping someone can help me with, but both are similar in nature.

Currently, I don’t know how to pass the existing parent $model to _form_escort_rates, thus in the action I’m retrieving it again which is not ideal.

So I’m trying to understand how I can pass the $form and $model from the parent view to the action rendered view?

It is absolutely correct. At time when you are making another request to the App by Ajax the first instance has already finished, it’s gone. So, you have to recreate all needed objects by some data (ids) you are previously send from “parent” App instance.
You can of course store these “parent” objects somewhere (in db, session etc.) but all in all you would retrieve them only by another way.

So if in my parent view I am defining $form as

 $form = ActiveForm::begin([
            'id'=>$model->formName(),
            'layout' => 'horizontal',
            'class' => 'form-horizontal',
            'fieldConfig' => [
                'enableError' => true,
                'options'=>['class'=>'']
            ]
        ]);

how do I recreate it in this dynamically generated child view? I don’t want to create a new active form, I need to bind to the existing one and that I’m not sure how to do.

Or am I overly complicating things, and I simple redeclare it since I have the $model?

$form = ActiveForm::begin([
            'id'=>$model->formName(),
            'layout' => 'horizontal',
            'class' => 'form-horizontal',
            'fieldConfig' => [
                'enableError' => true,
                'options'=>['class'=>'']
            ]
        ]);

And thank you so very much for your help!

how do I recreate it in this dynamically generated child view?

Your dynamically created child view is in another application instance, created every time from scratch. If you need to show the form in it you can instantiate the form in partial view as usual and return it on ajax request.

I don’t want to create a new active form, I need to bind to the existing one and that I’m not sure how to do.

You have to create new active form instance for reasons mentioned above. But do not repeat yourself, place the form definition in some separate partial view file so you can include it in both in “parent” an “child” view.

Of course as the form from example is powered by some model you can load it first so your form will be prefilled with attributes values in child view.

I’m sorry, I’m at a bit of a loss.

If I create a new form, then when the parent form is submitted, will this one also be submitted? They are all part of the same ‘record’ and I need all the data handled upon the parent form’s submission.

Both your forms exist in two distinct, disconnected from each other app instances.
First form is send with the first request. With second request (probably POST) you save form values in database. With third one (AJAX) you create new form, probably by loading model from database first, so from the user’s perspective it is looks like it is “the same form”. Each request is completely new application instance, you are creating everything new.