Form Display Post and Save using Widget

Hi Everyone,

I am new to Yii, I am using Yii version 1.1, I have tried to search forums and web for this answer, Please bear with me if I am asking a silly question here.

My Issue : I am using a view of a module. After that view I want to show one form of different module so that if someone submits it then data will be submitted and saved to database. I think the solution is using the Widgets (Please advise if you have better idea)

Now the Question is How can I use Widget to Display the form and after the form has been submitted the values should be stored in the database.

If you can give me some example also our can share some tutorial link that would also be great.

Your Help is very much appreciated.

Thanks and Regards,

Manu

hope this helps

http://www.yiiframework.com/extension/comments-module/

http://www.yiiframework.com/forum/index.php/topic/15723-module-widget-outside-of-the-module/

Dear Ali (alirz23),

Thanks for your help, I will try to work on this and will get back if I need any other info in this regard. Thanks again.

I am still looking for a widget post tutorial.

Warm Regards,

Manu

here is an other tutorial

http://www.yiiframework.com/wiki/713/how-to-create-a-widget-recent-comments-widget/

Dear Ali,

Thank you once again, Yes this tutorial I have read, this explains how we can show something (Recent Comments) using the widget, But the examples that you have given to me earlier helped me to some extents, thank you so much for your kind help. I really appreciate that. What I feel that there is a need of tutorial here which explains the widget in more detail, specially how to post something using the widget.

Thanks and Regards,

Manu

I don’t understand why you want to use a widget for your purpose.

Displaying a form and store the submitted data is standard code that can be created with gii.

Let gii create a model, a controller and the view’s for admin, update, create …

You can copy/paste and modify the generated code for your needs.

What do you need more?

Hi Joblo,

Thanks for your reply, but my scenario is different, I am not talking about the CRUD which is generated by Gii, Just think about the scenario, You have a view like Post and you want to add comments, So comments section (Block or Widget) can be called by some method in the view.

I have been reading lot of stuff here and as per the Yii Blog tutorial, by renderPartial method this can be accomplished. But I have been looking into other extensions, which has used the widgets methods for posting the form data. Please check here : http://www.yiiframework.com/doc/blog/1.1/en/comment.create

<?php $this->renderPartial(’_comments’,array(

        'post'=&gt;&#036;model,


        'comments'=&gt;&#036;model-&gt;comments,


    )); ?&gt;

Ali previously replied with this tutorial : http://www.yiiframework.com/wiki/713/how-to-create-a-widget-recent-comments-widget/#add-comment , this is interesting and nice one, the only thing is that if it can also give us some knowledge about the posting data using widget forms it would be great.

Actually I tried to comment on the above mentioned tutorial so that if the author have knowledge, then he can have some more information on how to post using the widgets, I am new and not allowed to post comments there.

Once again Thanks Joblo,

Kind Regards,

Manu

The tutorial "Creating and Displaying Comments" seems to be the right for you.

If you want to handle POST data from a form (based on a model) you always have to implement steps like below:

  1. Create a view, that displays the form with a submit button to send data to this controller action.

  2. Create a corresponding controller action that handles the submitted data:

  • create a model: $comment=new Comment;

  • assign the $_POST data to the model attributes: $comment->attributes=$_POST[‘Comment’];

  • Validate/Save the model: if($comment->save())…

  • If no $_POST data from the form are submitted (or not valid or the model is not saved) render the form again with the model and the assigned attributes (display model errors).

Similar code is generated by gii in actionCreate.

If you understand the code generated by gii, you will understand the comment tutorial and you will be able to modify the code for your needs.

A widget is not needed, it cannot replace the controller action, only maybe display a form.

But why do you want to create a widget for displaying a form and not use renderPartial?