Reputation: 67
I'm having nice first days using Yii but have some troubles with getting things that i used to write 'from the finger' .
Here is the background for my question:
I created a 'project' model,controller and view (works fine) and in the 'views/project/_form.php' i put an ajax link that displays a partial view : a form that belongs to another model, named 'picture'.
So, the 'views/project/_form.php' has this inside:
<?php
echo CHtml::ajaxLink('Add picture', array('projekt/addPicture'),
array('update'=>'#req_res')
);
?>
<div id="req_res">...</div> <!-- here the ajax form shows -->
The /project/addPicture action successfully shows the form using such code:
public function actionAddPicture()
{
$model=new Picture;
$this->renderPartial('/projekt/_addPicture', array('model'=>$model));
Yii::app()->end();
}
and the '_addPicture.php' file in the /projekt/view folder has a "cactiveform" inside, loading the '$model' correctly (i can see my data stroed in the DB etc.)
Problem:
When i hit 'save' on the newly generated form, Yii sends me to a new white page containing again this form instead of saving it. Here's the URL Yii sends me to:
http://localhost/projekt/addPicture?_=1304644637668
I am vaguely aware that it may have something to do with me not telling Yii which controller to use (?) but i don't know how to specify the controller (if this is the case)
Can you point me to some direction here? Thank you
Upvotes: 0
Views: 1981
Reputation: 13394
By uesing cactiveform you define an action as an option of cactiveform:
$form=$this->beginWidget('CActiveForm', array(
'action'=>Yii::app()->createUrl(...), ....
Usually the action is "connected" to the save button you see at the form.
When i hit 'save' on the newly generated form,
btw: I think your Ajax-Link will not be used, if you click "save"
me not telling Yii which controller to use (?)
..look at the "action" option from cactiveform
Yii sends me to a new white page containing again this form instead of saving it
That's right. Because by using the default "save" button Yii executes the action. If the action is actionAddPicture, actually you don't process the postes data.
That means, you need something like that (just an simple example, which you have to customize for your needs):
if(isset($_POST[<model>]) )
{ $model=new model();
$model->attributes=$_POST[<model>];
$model->save;
[...]
Upvotes: 1