General MVC Questions on PHP & Magento Validations
I have been using Magento & PHP CodeIgniter MVC for about 4 months. I am having a peculiar problem understanding where to set the validation logic for both CodeIgniter & Magento, only if I consider to follow the MVC architecture to its true nature & in a very proper semantic way.
First of all, I will start off with Magento:-
Say I have a "Assignment
" module in Magento, which is about Players & Tournaments both. I have 3 action methods, in this "Assignment
" module's controller, where I need to check & validate the following:-
- Player ID
- Tournament ID
This validation is also required in the "Assignment
" module's Block class, along with in the 3 action methods.
Also what does actual MVC architecture suggest in this case?
If possible, please provide a code snippet, highlighting the logic of your implementation.
Regarding CodeIgniter or any other PHP MVC Framework:-
Since Validation of anything using sessions is quite page-specific & since there is no concept of Blocks in CodeIgniter, so normally session validations & setting of session variables are done in the Controller class only.
So my query in this regard is that is this a correct approach & under what circumstances should I put setters of sessions & session validations in the Model class?
And again, what does actual MVC architecture suggest in this case?
And again, if possible, please provide a code snippet, highlighting the logic of your implementation.
I am really in confusion state regarding this & I'm sort of stuck in here. Please help me in here. Any solution / help is greatly appreciated. Many thanks in advance.
EDIT:-
If possible, please provide some Mag开发者_JS百科ento code on how to create & write proper Validations, along with throwing Exceptions, with the actual Exception messages defined in the Model methods?
The Model layer should be able to maintain its own consistency, so regardless of other decisions, you should include your validations in the Model layer. To help the user (and provide more helpful validation messages), you may also wish to do some validation in the controller level.
The advantage of this approach is that, assuming you maintain your model layer, there is no chance of a rogue controller setting bad data. This plays into your second question, for which the Magento answer would be to use an object to manage data in parts of the session, and to validate that data on the way into the session.
As an aside, to deal with validations in your Blocks/Views, consider using validation like this. It has its own flaws, but generally minimizes the amount of validation code you have to write:
// make sure that the below returns the relevant assignment model class
$assignment = $this->getAssignment(); // or get it via a session, or helper, or what have you.
$player = getChosenPlayer();
try {
$assignment->setPlayer($player); //throws exception when invalid
... do more ...
$assignmnent->save();
} catch(SomeException $e) {
addValidationError($e->getMessage());
renderPageAgain();
}
精彩评论