Validation within workflow in Joomla component with MVC pattern
I'm writing a custom component for the Joomla CMS using the MVC pattern. I'm implementing a simple workflow, where I want the user to enter some data into a view, validate this data (on server side, this is not about client side/JavaScript validation) and redirect the user to another view, if the validation succeeded. If the data is invalid, the first view should be displayed again with the invalid fields marked.
Now I'm not sure, where to place the validation code: My first thought was to place it in the controller, since this one has to decide whether to redirect to the second view or not. But I found a couple of advices telling me not to place validation code in the controller.
Placing it into the model of the first view would also be an option. But since in Joomla there's a 1-to-1 relationship between model and view, I'm not sure if it's ok to only create a model class (because in case of successful validation, the first view is not shown again).
Is there any standard way to do this? Or is this kind of behaviour implemented in some standard component, so I could look for the source code? Any ideas (some pseudo-code would b开发者_如何学编程e nice and sufficient...)?
Thanks for any help!
In my projects, I put them in the Form model (using HTML_QuickForm2).
Semi code:
$form = new HTML_QuickForm2();
$form->addElement('text')->addRule('email');
if ($form->validate()) {
echo "all fine, storing in database";
} else {
//error, let's try it again
echo $form;
}
This way, you have all the rules and validations in your form object. Move the form initialization code into an own class extending HTML_QuickForm2, and you're set.
精彩评论