MVC - user input confirmation: controller, model, or both

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?

+4
source share
2 answers

The only thing your controller should take care of is to transmit the model data and (optionally) how to ask if they are valid or not. Actual validation must be performed inside the model because:

  • This is your data validation responsibility model. .
  • You want to be able to reuse this model without repeating yourself.
  • You want to be able to replace the model object (for example, define a subclass with a completely different set of validation rules), and your controller should not care.
+1
source

It depends.

The best option for DRY (do not repeat yourself), tested in the model.

But. There may be times when the validation you need to perform can be tailored and specific to a particular kind, and although these views may have different requirements, the input from both may be valid for the model.

Often, our models validate secure input based on a database schema, while a controller that can validate through a form or even a model method may be related to a much larger, specific validation.

Validating form input for your specific purpose and validating the model is not always the same.

0
source

Source: https://habr.com/ru/post/1495838/


All Articles