I know that questions like this were asked at different times in stackoverflow, but even after reading them I remain confused. I would like to get clarity as to where the validation form should be verified, demonstrating the problem with the example.
Let's say I have a form on my website with a field that someone fills out and then submits. The model would like the controller to pass this value correctly in order to process this value internally. The model gets input through the getInput function, which sets the following rules:
- The input must be a string type.
- The input must be greater than 0 and less than or equal to 100 characters.
- The input must match the email address pattern.
I suggest that I should throw an exception inside getInput if any of these conditions do not occur; in the end, the controller passed a value that did not comply with the rules that were set by the model.
Besides the above rules, the controller (at least I'm sure this applies to the controller) must also check if the initial value was set before the script continues with the other three rules.
Now my question is: which of these (4) rules should be checked by the controller, and which should be checked by the model? Obviously, the controller knows what the model is requesting so that it can adapt to this (and if it is not, it may encounter the consequences of the generated exception). On the other hand, it seems redundant to have several controllers that use the model and its getInput , checking the same line to ensure that it matches the rules set by the model. In addition, if the controller first checks whether the input has the correct length, for example, - and the model immediately does the same immediately after that, even more redundancy appears after calling getInput .
In this example, the controllers and the model can be seen as a partnership with the model, who is a grumpy perfectionist who will check the input that he receives from the controllers, regardless of the actions of these hearty partners who try to supply him with all his desires. But is such an attitude terribly inefficient?
source share