1

Upon form submission, if model is found in controller, a view is rendered with a set flash message but also with a customized message like hello <?= $model->username; ?> when applicable.

Everything worked fine, until I decided I'd be fun to add return $this->refresh(); to prevent form resubmission. Which ultimately throws Trying to get property of non-object in the view as model is righfully null.

As I see, the redirect method prevents the render method from being executed, hence the model variable from being sent to the view.

I'm using POST http method, I guesss I could change to GET if necessary. Have you any idea how to rework this?

CONTROLLER

public function actionIndividualSearch() {
        
        $model = new Order();
        $model->scenario = Order::SCENARIO_SEARCH;

        if ($model->load($post = Yii::$app->request->post()) && $model->validate()){
        //if ($model->load($post = Yii::$app->request->get()) && $model->validate()){

            $model = Order::find()->where(['number' => $post['Order']['number']])->one();
            
            $flash = $model ? ($model->status == Order::STATUS_COMPLETED ? 'orderCompleted' : 'orderNotCompleted' ) : 'orderNotFound';
            Yii::$app->session->setFlash($flash);

            return $this->refresh();
            //return $this->redirect(['', 'model'=>$post['Order']['number']]);
        }

        return $this->render('individualSearch', [
            'model' => $model,
        ]);
    }

VIEW

<?php

use yii\widgets\DetailView;
use yii\helpers\Html;

?>

<div class="page-header">
    <h1>Consulta tu Orden</h1>
</div>

<p>Por favor introdusca el número de orden impreso en su ticket.</p>

<?php echo $this->render('_search', ['model' => $model]); ?>

    <?php if(Yii::$app->session->hasFlash('orderCompleted')): ?>
    Hi <?= Html::encode($model->customer->first_name); ?> ...

    <?php elseif(Yii::$app->session->hasFlash('orderNotCompleted')): ?>
        Hi <?= Html::encode($model->customer->first_name); ?> ...
    
<?php elseif(Yii::$app->session->hasFlash('orderNotFound')): ?>
        Dear Customer...
    <?php endif; ?>
Decimoseptimo
  • 103
  • 11
  • Trying to get property of non-object: Which line it is showing error? Have you crossed check. Because, from here it seems ALL OK. – Nana Partykar Jun 22 '16 at 07:28
  • The issue is how to send the model from the controller to the view. As after `$this->refresh` it becomes null. [see](http://stackoverflow.com/questions/37959760/yii2-in-a-form-submission-model-is-null-after-redirect/37966072#comment63408655_37966072) @nana – Decimoseptimo Jun 23 '16 at 03:25

2 Answers2

0

After refresh your Order model is empty and $model->customer is null.

And you're trying to get $model->customer->first_name in view.

urmaul
  • 7,180
  • 1
  • 18
  • 13
  • Yeah i think I implied that in the question. The true question is how do I make the model available again? one way I can think of is creating it as a controller variable and then in the view access it like `$this->context->model`, I think that could do. Other way is changing the form to GET so that the model is always implied as an `ID` query parameter, BUT in this case, on every page reload/refresh the form would be re-submited, right?, the flash message would always be generated... @urmaul – Decimoseptimo Jun 23 '16 at 03:16
  • Ususally you have separate "View order" page and redirect to it instead of refreshing. – urmaul Jun 23 '16 at 12:38
0

The answer lied in using session variables, the flash type specifically. This allowed me to preserve variables between requests. Thanks to all involved.

Decimoseptimo
  • 103
  • 11