MVC模式中的验证层 [英] Validation Layer in MVC pattern

查看:81
本文介绍了MVC模式中的验证层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在哪里验证模型将使用的数据的最佳位置.例如,考虑注册表格.我们有一些数据来自注册表.因此,哪里是验证此数据的最佳位置.我们应该通过if语句或特殊的验证器类检查每个数据,这意味着需要大量编码,因此我想知道在哪里进行此操作.

Where is the best place to validate data which will be used by model. For example, think about registration form. We have some data which come from registration form. So where is the best place to verify this data.We should check every data by if statements or special validator class, and this means lots of coding, so i want to learn where is the place to do this.

在控制器中?或模型?

都是吗? 因为某些数据应该由其他模型验证?

Both? Because some of the data should be validated by other models?

如果您不确定确切的答案,请尝试找出两种方式的可能的优点和缺点.

If you are not sure about exact answer, please try to find possible advantages and disadvantages of both ways.

推荐答案

验证数据的来源应在模型中,而实际检查应在视图级别(可能使用javascript或UI提示)和在模型级别.纯粹主义者会建议不要参与该观点,但我不同意.

The source of the validation data should be in the model while the actual checking should probably be done at both the view level (perhaps with javascript or UI hints) and at the model level. Purists will suggest that the view should not be involved but I disagree.

这篇关于MVC模式中的验证层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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