模型/视图/控制器模型中验证的最佳位置? [英] Best Place for Validation in Model/View/Controller Model?

查看:177
本文介绍了模型/视图/控制器模型中验证的最佳位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个广泛使用MVC设计模式的PHP项目。我正在寻找一个表单的验证,并且好奇的是,正确的验证地点是什么。



由于生成表单的方式,对回发数据的验证在查看组件中要简单得多,重复性更低。是否可以让视图验证响应数据,还是应该在控制器内甚至模型中实现?



有什么好处?

解决方案

如果您验证客户端的数据(即Javascript验证),这绝对不够,并且根本不安全,您应该实现它在视图中。



如果您正在验证服务器端的数据,并且您的验证不需要应用程序业务逻辑(即,您没有检查用户是否具有



如果验证需要业务逻辑,请在模型中实现,并通过控制器调用。 >

回发验证不是很好,因为它有很大的压力和延迟,唯一的优点是程序员(不被考虑)。



您可以使用正则表达式进行大多数验证,具有相同的语法(al大多数)在PHP和JS上。


I am working on a PHP project which makes extensive use of the MVC design pattern. I am looking to add validation to a form and am curious as to what the right place for validation is.

Due to the way that forms are generated, validation on postback data is a lot simpler and less repetitive in view components. Is it acceptable to have the view validating response data, or should this be implemented within the controller, or even the model?

What are the benefits?

解决方案

If you're validating the data on client side (i.e Javascript validation) which is absolutely not enough and not secure at all, You should implement it in View.

If you're validating data on server side, And your validation does not require application business logic (i.e you're not checking to see if the user has enough credit in his account), You should validate in the controller.

If the validation requires business logic, Implement it inside the model and call it via controller.

Postback validation is not good since it puts lots of pressure and delay, And the only advantage is to the programmer (not to be accounted).

You can use regex for most of validation, Which has the same syntax (almost) on PHP and JS.

这篇关于模型/视图/控制器模型中验证的最佳位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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