验证应该在Form对象还是模型中完成?

编程入门 行业动态 更新时间:2024-10-25 16:23:13
本文介绍了验证应该在Form对象还是模型中完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这个问题主要针对PHP中的Zend,尽管它当然也适用于其他语言和框架,所以我欢迎大家的意见.

This question is mainly geared towards Zend in PHP, although it certainly applies to other languages and frameworks, so I welcome everyone's opinion.

我只是最近才使用Zend框架,虽然它并不完美,但我在其中度过了一段愉快的时光.但是,令我抓狂的一件事是,我看到的使用Zend的大多数示例都使用v 在特殊形式的对象中进行的替代,而不是在模型中进行.我认为这是一种不好的做法,因为数据可以通过表单输入以外的其他方式进入系统,这意味着要么必须弯曲验证器以扭曲其他输入,要么必须在第二个位置进行验证,并且逻辑必须重复.

I've only recently been using the Zend framework, and while it's not perfect, I have had a pretty good time with it. One thing that drives me crazy, however, is that most of the examples I see of people using Zend do the validation in special form objects, rather than in the model. I think this is bad practice because data can enter into the system in other ways beyond form input, which means that either validators have to be bent and twisted to validate other input, or validation must be done in a second place, and logic duplicated.

我在那里发现了其他一些帖子和博客,这些人和我的想法相同,但是Zend的开发人员做出此选择是有原因的,而其他人似乎毫无疑问地使用了它,所以我想在这里获得社区的一些反馈.

I've found some other posts and blogs out there with people who feel the same way I do, but the developers of Zend made this choice for a reason, and other people seem to use it without issue, so I wanted to get some feedback from the community here.

正如我所说,这主要适用于Zend,尽管我认为从整体上看这个问题很重要,而不是在Zend框架范围内工作,因为Zend的设计目的是您可以使用尽可能多的内容,或按您希望的那样少.

As I said, this mainly applies to Zend, although I think it's important to look at the issue as a whole, rather than working within the confines of the Zend framework, since Zend was designed so that you could use as much, or as little, as you wished.

推荐答案

这是一个非禅定的答案,但是我认为该模型应该负责其自身数据的有效性.如果是这种情况,则验证属于模型,但是可能并非始终可以实现,并且可能有必要在视图中执行验证,但是我认为这应该是在模型中执行的验证之外,而不是替代为此.

This is a non-zend specfic answer, however I believe that the model should be responsible for the validity of its own data. If this is the case then the validation belongs in the model, however this may not always be achievable and it may be necessary to perform validation in the view, however I think this should be in addition to the validation performed in the model not a replacement for it.

仅在视图中进行验证的问题是,在某些时候您可能希望在数据上使用另一个视图.您的站点可能会变得很流行,并且客户正在请求基于XML的API来生成自己的视图.然后,您是否依靠客户来验证数据?

The problem with only having validation in the view is that at some point you will probably want another view on your data. Your site may become popular and customers are asking for XML based APIs to generate their own views. Do you then rely on the customer to validate the data?

即使您不必提供API,某些客户可能仍需要定制的视图,这些视图足够不同以保证页面的版本完全不同,现在您仍然可以在重复的视图中进行验证.

Even if you do not have to provide APIs some customers may want customized views that are sufficiently different to warrant a completely different version of the page, again you now have validation in the views duplicated.

我认为理想的情况是让您的模型进行验证,但要使验证结果可供视图读取并再次显示显示验证结果的页面.

I think the ideal scenario is to have your model do the validation but to make the results of the validation available for the view to read and render the page again with the validation results displayed.

如果您想立即向用户等显示验证数据,我认为让视图进行验证是完全合理的,但是关于数据有效性的最终决定应由模型决定.

I think it is perfectly reasonable to have the view doing validation if you want to instantly display validation data back to the user etc but the final decision on data validity should rest with the model.

更多推荐

验证应该在Form对象还是模型中完成?

本文发布于:2023-10-27 11:42:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1533257.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模型   对象   Form

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!