General MVC Questions on PHP & Magento Validations General MVC Questions on PHP & Magento Validations codeigniter codeigniter

General MVC Questions on PHP & Magento Validations


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();}