MVC:模型或控制器是否验证用户输入
MVC 中的哪一部分对用户输入进行验证?例如,用户注册系统,用户在视图中输入数据,用户的输入在哪里被清理并验证正确的输入,例如。正确的电子邮件,应用 php 清理功能..这会发生在控制器或模型中吗?这将决定返回哪些错误,
谢谢
What part in the MVC does user input get validated? For example, user registration system, the user inputs data in the View, where does the user's input get cleaned and validated for the correct input, eg. correct email, applying php cleaning functions..would this happen in the controller or the model? and which would decide what errors are returned
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据经典的 MVC 模型(图形应用程序),用户输入也是一个模型。大多数 PHP 框架都遵循 Passive-MVC 或 MVC-2 模型,其中它是控制器或控制器助手的域。做看起来最容易维护的事情。
As per the classic MVC model (graphical apps), user input is a model too. Most PHP framweworks follow the Passive-MVC or MVC-2 model, where it's the domain of the controller or controller helpers. Do what looks most maintainable.
在我看来,这完全取决于您想要执行哪种验证:
1. 如果您不希望字段为空或采用特定格式,我将在视图层上进行检查。这是大多数正则表达式可以应用的地方。只有当用户输入有效时,我才会将控制权传递给控制器以进行进一步的业务逻辑处理
2.如果我想确保用户输入(例如用户名)是否唯一,我将在控制器端进行验证并将任何反馈传递回视图。在后者中,控制器可能依赖于数据访问层或服务层或任何其他控制器助手的抽象。
3、使用方法上仍需合理化。
In my opinion, it all depends on what kind of validation you want to perform:
1. If you don't want a field to be empty or be in a specific format, I will do that check on the view layer. This is where most regex could be applied.Only once the user input is valid, is then that I will pass control to the controller for further business logic processing
2. If I want to ensure that a user input(, say a username) is unique or not , I will do that validation on the controller side and pass any feeback back to the view. In the latter, the controller might have a dependency on an abstraction of a data access layer or service layer or any other controller helpers.
3. Still have to rationalize on the approach to use.