Not able to access variable in view passed from an action in controller

Hi

I am recently started working on yii2. I’ve to display 3 reports, basically 2 different graphs and a tabular reports in a tabs view.

I was trying to render a view in controller and generate content based on the parameters. So I thought of doing this in this way:

  • Calling the render method with view name and params array with ‘graphtype’, model(for form) and in a particular case I also add a data array(filtering conditions) to params.



$model = new SearchForm();

return $this->render('accounts', array('graphType'=>'detail', 'model'=>$model));



  • ViewFile: Actually I have a single view file for all graphtypes, in which I have form displayed with model attributes and the report based on the graphtype. Since the form content is same for all the reports I have put that part of commonly in this view file (form.php)and added three other view files based on the graph type. I am using the renderpartial method to display this content based on the graph type.

Till here I am fine. I am able to see the graphs and reports.(when I don’t have the dataFilter in the array) After I added the filtering data to the params array of the render method and calling renderPartial with that array,




$model = new SearchForm();

$dataFilter = array();

$dataFilter['date'] = Yii::$app->request->get('date');

$dataFilter['type'] = Yii::$app->request->get('type');

return $this->render('accounts', array('graphType'=>'detail', 'model'=>$model, 'dataFilter'=>$dataFilter));



I am getting the below error:

[color="#FF0000"]exception [/color]‘yii\base\ErrorException’ with message ‘Undefined variable: dataFilter’ in D:\Program Files\Apache Software Foundation\Apache24\htdocs\sample\views\site\accounts.php:32

view code is as follows:




print Yii::$app->controller->renderPartial('form', array('model'=>$model));


if(isset($graphType) && !empty($graphType))

{

	echo '<br /><div>';

	$contentCloud = Yii::$app->controller->renderPartial('cloud');

	$contentChart = Yii::$app->controller->renderPartial('chart');

	$contentDetail = Yii::$app->controller->renderPartial('detailView', array('data'=>$dataFilter));

	

	switch($graphType):

	{

		case 'cloud':

			print $contentCloud;

		break;

		case 'cloud':

			print $contentChart;

		break;

		case 'cloud':

			print $contentDetail;

		break;

	}

	echo '</div>';	

}



Help me to know what went wrong in here. I don’t understand why I am not able access that third element in the array.

I figured out the issue. It is related to the situations where the dateFilter can be empty.

As of now I am getting them in GET in one particular UI. But I’ve forgotten to handle the other cases where the GET is empty. This is what causing the issue.

I am good now. Thanks if anyone spent time on this and sorry for the trouble if caused. I should have looked at it in a little bigger picture.