验证表单数据的最佳位置在哪里? [英] Where is the best place to verify form data?

查看:164
本文介绍了验证表单数据的最佳位置在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题很简单我应该在mvc设计模式(Zend,Symphony,Cakephp,Codeigniter)中进行表单验证过程。我问这个问题是因为我有自己的框架。但是我无法决定我应该在哪里处理表单验证。例如,我可以在控制器中编写if语句,但这会使控制器胖,或者我可以在模型函数中添加验证语句(在User :: register(,,)中)。当然这并不是很复杂的事情,但我的目的是要了解在企业级应用程序中如何做。

解决方案

如果您将MVC中的模型视为我们在.NET世界中调用ViewModels(而且我相信在其他地方称为演示模型)而不是域模型,该模型将是添加验证/验证的好地方。



这将允许您将验证逻辑重用于您正在重用该模型的任何位置,因为模型将将验证逻辑与数据一起封装,这是有道理的。这听起来像是对我的高度凝聚力。



作为一个健全检查,ASP.NET MVC框架似乎也朝着这个方向前进。由于这个问题被标记为php,我不知道这最后一条信息是加强还是削弱了这个论点。


Question is simple . Where should I put form verification process in the mvc design pattern (Zend , Symphony, Cakephp, Codeigniter) . I ask this question because i have my own framework. But i can not decide where should i handle form verification . For example , i can write if statements in controller but this makes controller "fat" , or i can add verification statements in model function ( in User::register( , , ) ) . Of course this is not very complex thing ,but my aim is to understand how do they do in enterprise level applications .

解决方案

If you consider the Models in MVC as what we in the .NET world call ViewModels (and I believe are called Presentation Models elsewhere) instead of Domain Models, the Model would be an excellent place to add validation/verification.

This would allow you to reuse the verification logic anywhere you are reusing the Model, and it makes sense since the Model would be encapsulating the verification logic together with the data. That sounds like high cohesion to me.

As a sanity check, the ASP.NET MVC framework seems to be heading in that direction as well. Since the question is tagged 'php' I'm not sure this last piece of information strengthens or weakens the argument.

这篇关于验证表单数据的最佳位置在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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