Redirect/Render the 'create' page again after form submit

i have a create view which i want to render again after it is submitted to create another record.

But POST is not clearing its values what i submitted on the last time and refreshing the page in the browser populates the fields again & i get errors.

i tried unset(), setting values inside post to nothing or blank array but nothing flushing it.

so how to do this?

Are you using Yii 1.0.x (as you posted in that section)?

Can you post your controller code to see how you submit to create another record?

No i am using 1.1.5, by mistake got submitted to 1.0. Can it be shifted to 1.1.x?

Here is my create action code:


function actionCreate() {

	$model = new Clients;

	$model->client_services = array();


	if (isset($_POST['Clients'])) {

		$model->attributes = $_POST['Clients'];

		if ($model->validate()) {

			$a = implode(',', $_POST['Clients']['client_services']);

			$model->client_services = $a; // save service list in a comma saperated values

		}


		if ($model->save()) {

			unset($_POST['Clients']);		// not flushing

			$model = new Clients;

		}

	}

	$this->render('create', array('model' => $model,));

}



[EDIT: removed some code line causing confusion=> $_POST[‘service’], $mn ]

Moved to proper section…

I see your code…

If you understand the concept here… in the view code you are using something like $model-><variablename>

In the controller you set the model variables to the values passed by the view… so that they can be used in the view…

so even if you unset the $_POST array… the model attributes are holding their values… so you need to reset the model values… use $model->unsetAttributes()

but to me it’s not clear why you render the same “view” file for different models… and the line $model->new $mn… as for this code you posted here… $mn is not defined… then again you are using here $_POST[‘service’] AND $_post[‘Clients’] so it seems you have a view for two models…

Can you post even the view code (if the above explanation does not give you the solution)…

Actually in the client form we also select the services adopted by him. so checking $_POST[Client][Services].

plz check the code again, i removed those lines causing confussion. actually i have written my own XAction base class extending CAction & then by extending XAction written createAction & all other action classes so the code is so devised in the XAction that the same createAction class can be used by all the controllers even if each model needs different treatment for saving the attributes. so thats why there got some mix-up in rewriting it again.

Anyways…

to simplify the code for testing i wrote the actionCreate() in the client controller again & posted here.

Yeah what u said the concept its very clear to me, but

$model->unsetAttributes() does not solve the problem coz if save is successful i am already sending a new instance of Client to the view.


if ($model->save()) {

       $model = new Clients;

  }



so $model now contains no previous data & the view also render with all fields blanks. But POST keeps the last submitted data.

TO SIMPLIFY there is 2 things happening when the create form rendered again after save:

  1. if i click ‘CREATE’ it POSTS new values & creates new row in the table with new data coz model gets new $model=new Clients; on save()

  2. but if user refresh the browser page POST submits the data it has already got which is already saved.

POST flushes its data only if we redirect to some other page or click the submit button again, untill then it retains the old values.

So if we clear those values, $_POST['Client], POST will not try to submit even if we refresh the page.

some way if we can tell the POST that its a new page by setting the HEADER data or something…

OK… I understood now your problem… the main problem is if a user "refresh" the browser…

I don’t think it’s a problem in the $_POST… the problem (I think) is in the browser… the browser remembers the values that was previously set…

Edit:

You could prevent that if you "redirect" to the same controller/action…

And here is a similar topic on this problem - http://www.yiiframework.com/forum/index.php?/topic/11849-page-refresh-and-form-re-submission/

… earlier i thought for redirect but i got confused to how to send the new model’s data into it. but after the last post i thought, why there is a need to send the data! i will again come to the create page from the URL.

i can also use forward(). i’ll just give it a try.

thanks for the discussion

hi!

could you solve this problem? because I’m struggling with the same issue now.

Thanks for any feedback!

BR

c

Based on this article, I have a similar solution. This is the example:




    public function actionBatchUpdate() {

        // retrieve items to be updated in a batch mode

        // assuming each item is of model class 'Item'

        $items=$this->getItemsToUpdate();

        if(isset($_POST['Item'])) {

            $valid=true;

            foreach($items as $i=>$item) {

                if(isset($_POST['Item'][$i]))

                    $item->attributes=$_POST['Item'][$i];

                $valid=$item->validate() && $valid;

            }

            if($valid)  // all items are valid

                // ...do something here

        }

        // displays the view to collect tabular input

        $this->render('batchUpdate',array('items'=>$items));

    }



The problem is, I don’t know what is going on at the end of the process: if($valid)…

if I don’t put any redirect here:




    if ($valid) {

        foreach ($models as $model) {

            $model->save();

        }

    }



models are saved fine, I get validation errors fine, but the form stays filled. It’s not good for me.

if I put redirect here (I think it should be here):




    if ($valid) {

        foreach ($models as $model) {

            $model->save();

        }

        $this->redirect...

    }



models are saved fine, but I get validation errors for my unique rule that these records already exist. it seems like there is model save for a second time or what?

and if I put redirect here:




    if ($valid) {

        foreach ($models as $model) {

            $model->save();

        }

    }

    $this->redirect...



models’ save and redirect goes fine, but I never get validation errors.

I have tried to play with unsetting $_POST, but I have no clue what is missing. what am I missing?

UPDATE: maybe it’s important, that I would like to redirect to the same page what is basically rendered.

I’m nost sure I need redirect anyway.

Interesting thing is, that I get only my unique validation error back when I do this in grid for the checkboxcolumn:

'checked' =&gt; '&#036;_POST[&quot;rendelesGyartmanyId&quot;][&#036;row]',

I do this, to make sure that when a user checks a lot of checkboxes, and there are any validation errors, and the page is reloaded, it’s not necessary to recheck checkboxes, but maybe it’s not a good solution.

UPDATE_2: the problem is maybe, that after submit, and redirect to same view, POST is still set, and values are still in there. that’s why Yii wants to submit again, but as I have a unique rule defined, I get the validation error. Is it a common behaviour of Yii, that after dealing with POST data it HAS TO be redirected to somewhere else? Is there no other way to somehow redirect to same page as a clean start? I’ve tried to unset models’ attributes, and POST, but I couldn’t solve it, but maybe I was just not doing it the good way.

UPDATE 3: very interesting. if I comment out model save here:




    if ($valid) {

        foreach ($models as $model) {

            // $model->save();

            // Static::model()->function();

        }

        $this->redirect...

    }



redirect is recognized and working, but as soon as I remove commenting out, redirect is not recognized. I’ve put a false view there and this way I could see that Yii doesn’t recognize it. What is the connection between code lines in foreach and redirect after foreach.

Can somebody please make it clear for me what is going on here?

thanks a lot!

setting form widget’s enableAjaxValidation property to false solved my problem.