2

I am new to yii2. I am passing two variables into my create view which is partially rendered from _form. But somehow I am unable to pass these variables, and getting error.

What i have done?

In my action controller I have declared two global variables

global $the_meter_id;
global $the_meter_msn;

Then in my else case condition I am getting the current id and current serial number.

else{//$meter_alive condition else
                    \Yii::$app->getSession()->setFlash('error',  '
 <div class="alert alert-error alert-dismissable">
 <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
 <strong>Validation error! </strong> Try GPRS Communication Again.</div>');

                   $model->attributes=$_REQUEST['MetersInventoryStore'];

                   $sim = MetersInventoryStore::findOne($_REQUEST['selected_meters']);
                   $the_meter_id = $sim->meter_id;
                   $the_meter_msn=$sim->meter_serial;

                   /*print_r($the_meter_msn);
                   print_r($the_meter_id);
                   die();*/

                    return $this->render('create', [
                        'model'=>$model,
                        'the_meter_id'=>$the_meter_id,
                        'the_meter_msn'=>$the_meter_msn

                    ]);
}

Then in my create.php I have done the following

 <?= $this->render('_form', [
    'model' => $model,
    'the_meter_id'=>$the_meter_id,
    'the_meter_msn'=> $the_meter_msn

]) ?>

And finally in my _form

<div class="selcted-meters">
        <input type="hidden" name="selected_meters" value="<?php echo $the_meter_id;?>"/>
        <p><b> <?php echo $the_meter_msn;?></b><br /></p>
        <a href="#" onclick="$(this).closest('selcted-meters').remove()">X</a>    <!--value="'+datum.id+'" />'+datum.meter_serial+'<a onclick="$(this).closest(\'.selcted-meters\').remove()">X</a></div>'-->
    </div>

The above code is written by following this solution. When I am running this code I am getting the error Undefined variable: the_meter_id in create.php

What I have done to make things run?

I have tried each and every possible way to render/pass my variables to the view, like putting the variables public and then getting the values direct in the render also tried to remove the variables from create.php but still getting same error.

Updated Code

I have updated my If condition and made the variables public

 public $the_meter_id;
 public $the_meter_msn;

In my action

if($meter_alive=='Yes') {
                    //$model->scenario ='Sim_installed';
                    if (isset($_REQUEST['selected_meters'])) {

                        foreach ($_REQUEST['selected_meters'] as $k => $v) {
                            $m = MetersInventoryStore::findOne($v);
                            $m->load(Yii::$app->request->post());
                            $m->status = MetersInventoryStore::$statuses[1];
                            if ($m->save()) {
                                Meters::change_status($m->meter_id, Meters::$status_titles[2]);
                                \Yii::$app->getSession()->setFlash('success',  '
 <div class="alert alert-success alert-dismissable">
 <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
 <strong>Communication is Verified </strong> </div>');
                            }
                        }

                        return $this->render('create', [
                            'model' => $model,
                            $this->the_meter_id,
                            $this->the_meter_msn
                        ]);
                    } else if ($model->load(Yii::$app->request->post()) && $model->save()) {
                        Meters::change_status($model->meter_id, Meters::$status_titles[2]);
                        return $this->redirect(['view', 'id' => $model->id]);
                    } else {
                        return $this->render('create', [
                            'model' => $model,
                            $this->the_meter_id,
                            $this->the_meter_msn
                        ]);
                    }
                }else{//$meter_alive condition else
                    \Yii::$app->getSession()->setFlash('error',  '
 <div class="alert alert-error alert-dismissable">
 <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
 <strong>Validation error! </strong> Try GPRS Communication Again.</div>');
                    $model->attributes=$_REQUEST['MetersInventoryStore'];
                    /*$model->attributes=$_REQUEST['selected_meters'];*/
                    $sim = MetersInventoryStore::findOne($_REQUEST['selected_meters']);
                  $this->the_meter_id =$sim->meter_id;
                  $this->the_meter_msn=$sim->meter_msn;

                   /*print_r($the_meter_msn);
                   print_r($the_meter_id);
                   die();*/
                   /*return $this->render('_form', array('the_meter_id' => $this->the_meter_id, 'the_meter_msn'=> $this->the_meter_msn    ));*/
                    return $this->render('create', [
                        'model'=>$model,
                        $this->the_meter_id,
                        $this->the_meter_msn

                    ]);
}

Then in my create.php

<?php

 use yii\helpers\Html;


 /* @var $this yii\web\View */
/* @var $model common\models\MetersSimInstallation */

$this->title = 'Create Meters Sim Installation';
$this->params['breadcrumbs'][] = ['label' => 'Meters Sim Installations',   'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;

 ?>


 <section class="content-header">
<h1>SIM Installation</h1>
</section>
<section class="content">
<div class="box">
    <div class="box-body">
        <div id="statusMsg" >
                     <?= Yii::$app->session->getFlash('error');?>
                     <?= Yii::$app->session->getFlash('success');?>
         </div>

<?= $this->render('_form', [
    'model' => $model,
     $this->the_meter_id,
     $this->the_meter_msn
]) ?>

    </div>
</div>
 </section>

Updated code 2

if( isset($_REQUEST['selected_meters']))
        {
            $m = MetersInventoryStore::findOne($_REQUEST['selected_meters']);
            $msn = $m->meter_serial; // current selected meter serial number is saved
            $date_time =  str_replace(' ','T',date('Y-m-d H:i:s')); // current date time
            $api_url = 'http://IP:Port/api/meters/GetByMsn/' . $msn . '/' . $date_time ; // my base URL

            $curl = curl_init($api_url);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: MY AUTHENTICATION CODE'));

             $curl_response = curl_exec($curl);
             $json=json_decode($curl_response);
             $meter_alive= $json->data->Response;

                if($meter_alive=='Yes') {
                    //$model->scenario ='Sim_installed';
                    if (isset($_REQUEST['selected_meters'])) {

                        foreach ($_REQUEST['selected_meters'] as $k => $v) {
                            $m = MetersInventoryStore::findOne($v);
                            $m->load(Yii::$app->request->post());
                            $m->status = MetersInventoryStore::$statuses[1];
                            if ($m->save()) {
                                Meters::change_status($m->meter_id, Meters::$status_titles[2]);
                                \Yii::$app->getSession()->setFlash('success',  '
 <div class="alert alert-success alert-dismissable">
 <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
 <strong>Communication is Verified </strong> </div>');
                            }
                        }

                        return $this->render('create', [
                            'model' => $model,
                        ]);
                    } else if ($model->load(Yii::$app->request->post()) && $model->save()) {
                        Meters::change_status($model->meter_id, Meters::$status_titles[2]);
                        return $this->redirect(['view', 'id' => $model->id]);
                    } else {
                        return $this->render('create', [
                            'model' => $model,
                        ]);
                    }
                }else{//$meter_alive condition else
                    \Yii::$app->getSession()->setFlash('error',  '
 <div class="alert alert-error alert-dismissable">
 <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
 <strong>Validation error! </strong> Try GPRS Communication Again.</div>');
                    $model->attributes=$_REQUEST['MetersInventoryStore'];

                    $sim = MetersInventoryStore::findOne($_REQUEST['selected_meters']);



                    return $this->render('create', [
                        'model'=>$model,
                        'the_meter_id' => $this->the_meter_id=$sim->meter_id,
                        'the_meter_msn' => $this->the_meter_msn=$sim->meter_msn
                    ]);


                }
        }
             return $this->render('create', [
                'model' => $model,

            ]);

After running the code I am getting the error Getting unknown property: yii\web\View::the_meter_id in create.php

Any help would be highly appreciated

5
  • Show me your create.php Commented Oct 25, 2017 at 6:43
  • @DoubleH kindly see the updated code I have placed my full create.php Commented Oct 25, 2017 at 6:50
  • 1
    is this is your complete create.php .. it seems you are not passing 'the_meter_id'=>$the_meter_id, 'the_meter_msn'=> $the_meter_msn to your _form.php Commented Oct 25, 2017 at 6:56
  • @DoubleH, yes sorry my mistake I have added them now Commented Oct 25, 2017 at 6:59
  • see my anwser below Commented Oct 25, 2017 at 7:02

1 Answer 1

2

In Controller use ..

return $this->render('create', [
       'model'=>$model,
       'the_meter_id'=>$the_meter_id,
       'the_meter_msn'=>$the_meter_msn
]);

Silly Mistake .. use

<?= $this->render('_form', [
    'model' => $model,
    'the_meter_id'=>$the_meter_id,
    'the_meter_msn'=>$the_meter_msn
]) ?>

Instead of

<?= $this->render('_form', [
    'model' => $model,
     $this->the_meter_id,
     $this->the_meter_msn
]) ?>

in your create.php

Sign up to request clarification or add additional context in comments.

6 Comments

again getting error Undefined variable: the_meter_id
What is complete Error this time
$this->render('_form', [ 'model' => $model, 'the_meter_id'=>$the_meter_id, 'the_meter_msn'=>$the_meter_msn In create.php
In action it shows return $this->render('create', [ 'model' => $model, ]);
In Controller use ` $this->render('create', [ 'model' => $model,'the_meter_id'=>$the_meter_id, 'the_meter_msn'=>$the_meter_msn ]);` when rendering view,,,
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.