Separated Functions For Multiple Models In Single Action

Probably the question is lame but I could not find the proper way of solving this case. And the case is:

I have one page with several forms and I need to separate the save functions from the action function and still get everything validated.

Ex:


	public function actionAdmin(){


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

			$this->_addPost();

		}


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

			$this->_addBlog();

		}

}


	private function _addPost(){

		$post=new Post('insert');


		if($post->save()){

			$this->redirect(Yii::app()->request->urlReferrer);

		}

	}


	private function _addBlog(){

		$blog=new Blog('insert');


		if($blog->save()){

			$this->redirect(Yii::app()->request->urlReferrer);

		}

	}




But to get both validated I need to pass the $post and $blog to be rendered.

Another option is to make separated actions for adding Blog and Post and to change the default form action. So the Post form will go to post_add, and the other one to blog_add. But here is the same case => can not validate the form and if the input is not valid either dont get redirected or if redirected no error massages are set.

Can it be solved with Yii?

Somethign is yelling "scenarios" at me …

nonono, I just want to separate the functions from the main action

If i understand correctly you can either post a Blog model or a Post model or both at the same time??

maybe something like this?


public function actionAdmin(){


                $ok = false;

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

                        $ok = $ok && $this->_addPost();

                }


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

                        $ok = $ok && $this->_addBlog();

                }


                if($ok)

                {

                    $this->redirect(Yii::app()->request->urlReferrer);

                }


}


        private function _addPost(){

                $post=new Post('insert');


                return $post->save();

        }


        private function _addBlog(){

                $blog=new Blog('insert');


                return $blog->save();

        }

Ah, I see. Well, if I were you, I’d indeed go with different forms targeting different actions. Everything else is bound to become messy at some point.