MVC 软件架构中验证逻辑的放置位置
我其实已经开始学习mvc架构了。
我很困惑是否将用户名注册验证逻辑放在模型中或控制器中。
我有某种状态消息,可以告诉用户要注册的新用户名是否可用。
我开始感到困惑,因为大多数消息来源说它应该在模型中,因为它涉及在将用户名数据放入数据库之前进行验证(而不是检查用户名字段的输入)。但是,状态消息应该在用户按键或更改更改用户名字段之前立即响应,这使我认为它应该位于控制器中,因为它涉及更多用户事件。
我实际上关心的不是要使用的框架,而是涉及 MVC 的标准概念。根据上述条件/前提,我应该将用户名验证逻辑放在哪里?
I am actually starting to learn the mvc architecture.
I'm confused on whether to place my username registration validation logic in model or in controller.
I have some sort of status message that would tell the user whether the new username to register is available or not.
My confusion started because most sources say that it should be in the model because it involves the username data to validate before placing it on the database (rather than checking inputs to the username field). However, the status message should respond immediately prior to the change in the username field by user keypress or change, which lead me to think that it should be in the controller because it involves more on user events.
My concern is not actually on the framework to use but on the standard concept involving MVC. Where do I put the username validation logic based on the conditions/premise above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如 Shikhar 所说,实际检查名称是否可接受/可用是模型的责任。控制器可以提供由页面上的某些 AJAX 调用的操作,以便在按下每个键时,页面上的文本被发送到专用控制器操作,然后通过模型对其进行验证(任何涉及数据库的内容都是模型)。
在视图中需要考虑一些事情,例如当用户快速打字时,您应该在进行新的调用之前取消以前的调用,因为这可能会造成混乱。
此外,当用户在数据输入结束时提交表单时发生的控制器后操作应该执行与 AJAX 操作相同的验证,以避免用户之间的竞争条件。
As Shikhar says, the actual checking of whether the name is acceptable/available is a Model responsibility. The controller can provide an action that is called by some AJAX on the page, so that as each key is pressed, the text on the page is sent to the dedicated controller action which then validates it through the model (anything that touches the database is Model).
There are a couple of things to consider in the view, such as when the user is typing quickly, you should cancel the previous calls before making the new one as this can get confusing.
Also the controller post action that happens when the user submits the form at the end of their data entry should perform the same validation as the AJAX action did just to avoid race conditions between users.
它应该在您自己阅读的模型中。我认为您对“验证过程”和“验证规则”感到困惑。验证过程将在客户端的控制器中,但验证规则是模型的属性。
It should be in the model as you have read yourself. I think you are getting confused between the "validation process" and "validation rules". Validation process will either be in controller on the client side but validation rules are properties of model.
作为@Colin Desmond 的补充,模型实例永远不应该包含“错误”数据,因此,在我看来,在 MVC 环境中应该包含验证逻辑。这样,无论在何处创建模型实例,都永远不会使用错误的数据对其进行初始化,并且对模型实例进行操作的类可以依赖其数据。例外情况是是否存在依赖于视图的验证逻辑。视图相关逻辑(异常)应该在控制器中实现。
例如,应在模型中实现电子邮件地址的验证。但是,模型可能允许负金额的银行交易,但视图 A 可能不允许负金额的交易。此异常的逻辑应在控制器上实现。
As an addition to @Colin Desmond, a model-instance should never contain 'wrong' data and thus, in my opinion, in an MVC environment should contain validation logic. So that, regardless of the place where an instance of the model is created, it can never be initialized with wrong data and classes that operate on the model instance can rely on its data. The exception is if there is validation logic that is view dependent. View dependent logic (exceptions) should be implemented in the controller.
For example, validation of an email-address should be implemented in the model. However, a model might allow a bank transaction with a negative amount, but view A might not allow a transaction with a negative amount. The logic for this exception should be implemented on the controller.