Redirect from widget application design
#1
Posted 03 February 2012 - 08:35 PM
I'd like to talk about application design and MVC.
Suppose I have a form that can appear on many site pages. For example, Contact form, or Login form.
In order to keep it DRY, I'm thinking about creating a widget for this form and doing all the processing in this widget.
I have two questions:
1. Is it OK (from the point of app design and MVC) to make create/update operations in the widget?
2. Is it OK to call a redirect from the widget?
PS. Yeah, I know that I can use AJAX and partials for this, but the question is about widgets.
#2
Posted 04 February 2012 - 02:38 AM
You could just write an accompanying model for these widgets and pass them when you call the widget from in your view.
Sometimes we need to deviate from one convention to meet another, in this case DRY over MVC. If both really matter to you, I'm sure you can engineer a solution.
#3
Posted 04 February 2012 - 03:42 AM
You should create a LoginController and ContactFormController. I don't see how it can brake the DRY principle, I would say it follows the KISS principle
#4
Posted 04 February 2012 - 08:05 AM
andy_s, on 04 February 2012 - 03:42 AM, said:
We do the same in controller, right?
if (isset($_POST['MyModel'])) {
$model->attributes = $_POST['MyModel'];
....
}
Quote
Again, in controller we do the same thing. Take a look at actionCreate, for example.
Quote
Ok, let's consider the following situation.
User opens "list" page, i.e.
function actionIndex() {
$items = MyModel::model()->findAll();
$this->render('index', array('items' => $items));
}
I want to add contact form at the end of the list.
In order to keep DRY I should use partials or widgets for this.
Let's suppose I've created a separate controller (ContactController) for processing submitted form.
Everything goes well until user enters invalid data in this form.
We must display pre-filled contact form with errors.
How should we do it?
#5
Posted 04 February 2012 - 08:24 AM
Quote
But CWidget doesn't have accessRules() and filters() methods. I just want to say Yii's CWidget is not supposed to replace a controller, there were some discussions about it already but with no result.
Quote
We must display pre-filled contact form with errors.
I used to solve this problem with ajax validation. On some sites user is redirected to a separate login page if he enters invalid data.
If you are not fine with ajax validation, then I don't see a better (easier) solution than to put everything inside a widget, but, as I said above, widgets are not supposed to perform such operations, they are just to show some content. For example, we don't use CActiveForm to validate and save models, we just use it to render an html form.
Edit: related discussion, one more.
This post has been edited by andy_s: 04 February 2012 - 08:31 AM
#6
Posted 04 February 2012 - 09:00 AM
andy_s, on 04 February 2012 - 08:24 AM, said:
If you are not fine with ajax validation, then I don't see a better (easier) solution than to put everything inside a widget
Yes, I'm using ajax too for now.
My question is rather philosophic than real.
Quote
Thanks, I'll have a look.
#7
Posted 04 February 2012 - 09:39 AM
#8
Posted 04 February 2012 - 11:50 AM
DRY and KISS should come together and therefore you may want to just make the code that handle the logic of login as well as the code that handle the form submission data in a contact form to be reusable. In term of MCV, make the 'M' reusable. The answer to your 2 questions at the original post are YES but doing a widget will violate the KISS. A widget needs its companion view, and when you have 2 files in for something you want to be reusable, is not simple. You will find out that dealing the the CSS, making the the HTML generated code in that view is deadly hard and at the end, you your next project you will end up with modifying the views.
This is additional to the problem of filler has been mentioned. However, for my applications it's not the problem we can solve.
Created by Flexica team, Gia Han Online Solutions
#9
Posted 04 February 2012 - 02:25 PM
Hudson Nguyen, on 04 February 2012 - 11:50 AM, said:
Yes, and many more. For example, comment form.
Quote
So does extra controller.
For example, I may have CommentWidget + its view, OR CommentController + its view (partial?..).
But in case of controller, you will have to put form creation code in EVERY controller that displays form.
I mean, these lines:
$commentForm = new Comment;
$this->render('index', array('form' => $commentForm, ...));Someone was talking about KISS?
#10
Posted 04 February 2012 - 11:02 PM
Yes, that's too simple and not really much reused but it's applied to more scenario than login or contact form. You can of course spend weeks or months to make a nice and highly customizable login widget or contact widget but it's not productive. Look at all of the projects you did, if you've already made it then do the same thing in Yii (then share it here pls). Otherwise, Yii does not help you to do any magic here.
Created by Flexica team, Gia Han Online Solutions
#11
Posted 05 February 2012 - 09:44 AM
WidgetName::postProcess();which would do the actual redirect. To avoid strong coupling you could pass the URL for redirection and any model that you want to use via the widget properties. I think that with this approach not only you have the benefits of accessRules(),filters() and other controller goodies but also a reusable loosely coupled widget. For added security, you should check whether the posted widget name exists and perhaps the csrf.

Help














