在 Coldfusion 中创建通用服务器端验证函数

发布于 2024-11-19 15:40:00 字数 357 浏览 3 评论 0原文

所以,我一直在尝试清理我的代码并学习我应该始终做的事情......当然,服务器端验证是我应该始终做的事情之一。然而,当我拥有如此巨大的形态时会发生什么呢?我真的很想要一个通用函数,允许我传递数据类型和字段名称。在 Coldfusion 中是否有安全的方法来执行此操作?

我已经考虑这样做有一段时间了,但我已经走进了死胡同,无法在网络上找到任何有关执行此类操作的信息。 Coldfusion 似乎不提供此功能。

但是,我认为如果有一种方法可以在输入标记中指定具有字段数据类型的属性,那就太酷了。然后,如果 Coldfusion 将其与您的字段名称一起插入到一个结构中,那就太好了。

有没有办法实现这一点,或者有人可以详细说明进行服务器端验证的最有效方法?

So, I've been trying to clean up my code and learn things that I should always do...well of course server-side validation is one of those things that I should always do. However, what happens when I have this huge form? I really would like to have a generic function that allows me to pass the data type and field name. Is there a secure way to do this in Coldfusion?

I've been looking into doing this for a while, but I've come to dead ends and can't find any info on doing something like this on the web. It seems like Coldfusion does not offer this ability.

However, I think it would be cool if there was a way to specify an attribute in your input tags that had the data type of the field. Then, it would be uber nice if Coldfusion stuck it into a struct for you with your field names.

Is there anyway to accomplish this or can someone elaborate on the most efficient way to do server-side validation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰雪梦之恋 2024-11-26 15:40:00

如果CF也有这样的东西就太好了!好消息,多年来确实如此! :)

您正在寻找的是 cfinput (和 cfform)标签。此标签包含您想要的标签中的验证细节(英雄所见略同,对吧?)。您可以指定验证、错误消息、是否应该验证客户端或服务器端 - 各种巧妙的技巧。

检查此处的实现 - 它非常容易使用:

http: //livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_i_07.html

请注意,很多代码女主角讨厌 cfform / cfinput。事实上,如果正确实施的话,它们并没有什么问题。它可能会被滥用,并且并不适合所有解决方案,但工具箱中的所有内容都是如此。总的来说,对于大多数表单输入和验证情况来说,它效果很好。

如果您讨厌这个想法,另一种方法是使用 cfparam 的内置类型属性并捕获错误。

例如,在表单处理页面的顶部,您可以:

<cfparam name="form.cardNumber" type="creditcard">

当到达此位置时,如果该变量中的值不是该类型,它将抛出一个您可以捕获的异常。这使您不必编写 if() 和模式匹配。此外,如果没有内置类型,您可以指定正则表达式进行模式匹配。

以下是更多信息和支持的类型:
http://livedocs.adobe.com/coldfusion /8/htmldocs/help.html?content=Tags_p-q_01.html

让我知道您的想法!

That would be great if CF had something like that! Good news, it does, for years now! :)

What you're looking for is cfinput (and cfform) tag. This tag includes the validation specifics right in the tag like you're wanting (great minds think alike, right?). You can specify the validation, the error message, if it should validate client or server side - all kinds of neat tricks.

Check here for implementation - it's quite easy to use:

http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_i_07.html

Be warned that a lot of code diva's hate cfform / cfinput. In reality, there is nothing wrong with them when implemented correctly. It can be abused and it won't fit for every solution, but that is true of everything in the toolbox. By and large, for most form input and validation situations it works great.

If you hate that idea, another is to use the built in type attribute of cfparam and catch your errors.

For example, at the top of your form processing page, you can :

<cfparam name="form.cardNumber" type="creditcard">

When this is reached, if the value in that variable is not of that type, it will throw an exception that you can catch. This keeps you from having to write the if() and pattern matching. Additionally, if there isn't a type built in, you can specify a regular expression for pattern matching.

Here's some more information and the types supported:
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_p-q_01.html

Let me know what you think!

盗梦空间 2024-11-26 15:40:00

我鼓励您考虑使用像 CFWheels(或 ColdBox)这样的 ColdFusion 框架,它已经内置了很多此类功能,使开发变得更加容易。使用 CFWheels 是我作为开发人员做出的最佳决定之一,并且我的开发技能在过去一年中得到了显着提高。网上有一些很棒的截屏视频可以帮助您入门。 http://cfwheels.org/screencasts

I would encourage you to look at possibly using a ColdFusion framework like CFWheels (or ColdBox) which has a lot of this type of functionality already built in to make development a TON easier. Using CFWheels has been one of the best decisions I've made as a developer and my development skills have grown significantly over the past year. There's some great screencasts online to get started. http://cfwheels.org/screencasts

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文