Dear Friend
Let us consider a Model Comment.
1.Now we make all the users to visualize the form.
CommentController.php
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index','view' and 'create' actions
'actions'=>array('index','view','create'),
'users'=>array('*'),
),
.....other rules to follow....
- In _form.php, let us have two submitting urls.
If user is guest, we are directing the user to site/login if he clicks the submit button.
Additionally we are also submitting the class name of the model along with form data.
If user is authenticated, form submission occurs normally.
<!--other form elements-->
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save',array(
'submit'=>(Yii::app()->user->isGuest)?array("site/login"):"",
'params'=>array("class"=>get_class($model)),
)); ?>
</div>
In action login we are gathering the form data and storing it in session in the name of the class.
We also setting the return url to comment/create.
By storing it in class name, the following code also works for forms of other models also.
SiteController.php
public function actionLogin()
{
$model=new LoginForm;
//Now add these lines
if(isset($_POST['class']))
{
Yii::app()->user->setState($_POST['class'],$_POST[$_POST['class']]);
Yii::app()->user->returnUrl=Yii::app()->createUrl(strtolower($_POST['class']."/create"));
}
............................................................................
In controller we can get the values from session, if it exists.
public function actionCreate()
{
$model=new Comment;
if(Yii::app()->user->hasState("Comment"))
{
$model->attributes=Yii::app()->user->getState("Comment");
Yii::app()->user->setState('Comment',null);
}
.............................................................................
I hope this will help you.
Regards.