MVC - 用户输入验证:控制器、模型或两者 [英] MVC - User input validation: controller, model or both

查看:27
本文介绍了MVC - 用户输入验证:控制器、模型或两者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道像这样的问题在 stackoverflow 上已经被问过很多次了,但即使在阅读了这些之后,我仍然感到困惑.我想通过示例演示问题来明确应该在哪里处理表单验证.

I know questions like this one have been asked various times on stackoverflow, but even after having read those, I remain confused. I would like to gain clarity as to where form validation is supposed to be handled through demonstrating an issue with an example.

假设我的网站上有一个表单,其中有一个字段供人填写并随后提交.模型希望控制器正确地传递这个值,以便在内部处理该值.模型通过函数 getInput 接收输入,该函数设置以下规则:

Let's say I have a form on my website, with a field that somebody fills out and subsequently submits. The model would like the controller to pass it this value correctly in order to process that value internally. The model receives the input through the function getInput, which sets the following rules:

  • 输入必须是字符串类型.
  • 输入必须大于 0 且小于或等于 100 个字符.
  • 输入必须与电子邮件地址的模式相匹配.

我想我应该在 getInput 中抛出一个异常,如果这些条件中的任何一个不满足;毕竟,控制器传递了一个与模型设置的规则不匹配的值.

I suppose I should throw an exception inside getInput, if any of these conditions do not meet; after all, the controller passed a value that did not match the rules that had been set by the model.

除了上述规则之外,控制器(至少我相当确定这适用于控制器)还必须首先验证输入值是否已设置,然后脚本继续执行其他三个规则.

Apart from the aforementioned rules, the controller (at least I am rather certain this applies to the controller) must also validate whether the input value had been set in the first place, before the script proceeds with the other three rules.

现在我的问题是:这 (4) 条规则中的哪些要由控制器验证,哪些要由模型验证?很明显,控制器知道模型请求什么,因此它可以适应它(如果不知道,它可能会面临抛出异常的后果).另一方面,拥有多个控制器似乎是多余的,这些控制器都使用模型及其 getInput,验证相同类型的字符串,使其与模型设置的规则相匹配.此外,如果控制器首先验证输入的长度是否正确 - 例如 - 之后模型立即执行完全相同的操作,一旦 getInput 被调用,似乎会出现更多的冗余.

Now my question is: which of these (4) rules are to be validated by the controller, and which ones by the model? It is apparent that the controller knows what the model requests, so that it can adjust to that (and if it doesn't, it may face the consequence of an exception being thrown). On the other hand, it seems redundant to have several controllers, which all make use of the model and its getInput, validating the same kind of string so that it matches the rules set by the model. Also, if the controller firstly validates if the input is of the correct length - for example - and the model does the precise same thing immediatly after that, once getInput gets called, even more redundancy seems to arise.

在这个例子中,控制器和模型可以被看作是一个脾气暴躁的完美主义者模型的伙伴关系,他将验证他从控制器那里得到的输入,不管这些努力尝试的亲切合作伙伴的行为满足他所有的欲望.但是这样的关系是不是非常低效?

In the example, the controllers and the model could be seen as a partnership with the model being a grumpy perfectionist, who is going to validate the input he gets from the controllers, regardless of the actions of these cordial partners who try hard to supply him with all his desires. But isn't such a relation terribly inefficient?

推荐答案

你的控制器唯一应该关心的是如何将数据传递给模型,以及(可选)如何询问它是否有效.实际的验证应该在模型内部完成,因为:

The only thing your controller should care about is how to pass the data to the model and (optionally) how to ask it whether they're valid or not. The actual validation should be done inside of the model, because:

  • 验证将要使用的数据是您的模型的责任.
  • 您希望能够重复使用该模型而无需重复自己.
  • 您希望能够替换模型对象(例如,使用一组完全不同的验证规则定义子类)并且您的控制器不应该关心.
  • It is your model's responsibility to validate the data it is going to use.
  • You want to be able to reuse that model without repeating yourself.
  • You want to be able to able to substitute the model object (e.g. define a subclass with a completely different set of validation rules) and your controller shouldn't care.

这篇关于MVC - 用户输入验证:控制器、模型或两者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆