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

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

问题描述

我知道像这样的问题都被要求对计算器不同的时间,但即使是在看过这些之后,我依然迷茫。我想获得清晰度到表单验证应该通过展示一个例子的一个问题来处理。

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.

让我们说我有一个表单上我的网站,与某人填写并提交随后的字段。该模型希望控制这个值正确传递,以内部处理该值。

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 的,验证同一种字符串,使其设置的规则相匹配由模型。此外,如果控制器首先验证输入是否正确长度的 - 例如 - 和模型后立刻做了precise同样的事情,一旦 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:


  • 这是模型的责任来验证数据的将使用。

  • 您希望能够重用模型不重复自己。

  • 您希望能够能够替代模型对象(例如定义一个具有完全不同的验证规则的子类)和你的控制器不应该关心。

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

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