Coldfusion 服务器端验证有时需要有时不依赖于单选按钮

Coldfusion Server Side Validation Sometimes Required sometimes not depending on radio buttons

我正在尝试更好地了解服务器端验证。我一直在使用 JavaScript 编写大量客户端验证,但没有意识到如果用户只是关闭 JavaScript 整个应用程序不会验证任何字段。这就是我正在为 ColdFusion 服务器端验证所做的工作。

我想知道的是,您将如何根据让我们说单选按钮是或否来打开和关闭是否需要字段。假设您有一个单选按钮,如果选择是,则需要另一个输入,但如果选择否,则需要另一个字段。我只是想知道您如何使用 _cf 格式的开关来做类似的事情。您会只在隐藏字段上创建 if 语句来执行此操作吗?

我一直在研究它,只是在寻找一些关于人们如何实现这样的事情的意见,因为看起来你可以通过客户端做很多事情,但这一切都毫无意义,因为他们可以转向关了。

<table>
<tr>
    <td>Date:</td>
    <td><input type="text" name="date" size="20"></td>
    <strong class="delete me and add my line number to the highlight in my pre tag">
    <input type="hidden" name="date_cfformrequired" value="You must enter a date.">
    <input type="hidden" name="date_cfformdate" value="The date you entered is not a valid date."></strong>
</tr>
<tr>
    <td>Distance:</td>
    <td><input type="text" name="distance" size="20"></td>
    <strong class="delete me and add my line number to the highlight in my pre tag">
    <input type="hidden" name="distance_cfformrequired" value="You must enter a distance.">
    <input type="hidden" name="distance_cfformfloat" value="The distance you entered is not a valid number."></strong>
</tr>
<tr>
    <td>Time:</td>
    <td><input type="text" name="time" size="20"></td>
    <strong class="delete me and add my line number to the highlight in my pre tag">
    <input type="hidden" name="time_cfformrequired" value="You must enter a time.">
    <input type="hidden" name="time_cfformregex" value="^\d{1,3}:\d{0,2}$"></strong>
</tr>
<tr>
    <td>Comments:</td>
    <td><input type="text" name="comments" size="50"></td>
    <strong class="delete me and add my line number to the highlight in my pre tag">
    <input type="hidden" name="comments_cfformrequired" value="You must enter a comment.">
    <input type="hidden" name="comments_cfformmaxlength" value="50"></strong>
</tr>
<tr>
    <td colspan="2" align="right">
        <input type="submit" name="Add Entry">
    </td>
</tr>
</table>

您对客户端和服务器端验证之间差异的理解是正确的。考虑到客户端验证可以归结为一个特性。 Javascript 验证可以指导他们,而不是用户输入信息、提交表单,然后被告知有误,但因为它可以被禁用,所以依赖它并不好。

服务器端验证总是按规定进行。

代码很简单

举个例子 - HTML

Name:
<input type="text" name="myname">

Were you referred by a current user?
<input type="radio" name="Referred" value="0" checked> No
<input type="radio" name="Referred" value="1"> Yes

Who referred you?
<input type="text" name="ref_user">

表单处理

<cfset Err = {Messages = []}> // creates a struct named Err with an array names Messages.

<cfif len(trim(form.myname)) eq 0>
  <cfset ArrayAppend(Err.Messages,"Please enter your name!">
</cfif>

<cfif form.Referred eq 1 and len(trim(form.ref_user)) eq 0>
  <cfset ArrayAppend(Err.Messages,"You said someone referred you, who was it?">
</cfif>

<cfif ArrayLen(err.messages) eq 0>
  ...no errors, we can do form processing...
</cfif>

然后,当输出错误时,如果它们存在

<cfif ArrayLen(err.messages) gt 0>
    Sorry, an error occurred.<br>
    <cfoutput>
    <cfloop array="#err.messages#" index="cError">
      - #cError#.<br>
    </cfloop>
    </cfoutput>
</cfif>

在最后一个代码片段的第一行,您会在行尾看到 gt 0。这不是必需的,大多数人都会将其关闭。您似乎熟悉 javascript,cf 的工作方式与您可能会说的相同

if (Err.Messages.length) {
  ... show
}

在cf中,你可以做类似的,<cfif ArrayLen(Err.Messages)>

请记住 cfparam 您的变量或检查它们是否存在于您的表单处理中,就像这样..

<cfif not StructKeyExists(form, "Referred") or (form.Referred eq 1 and len(trim(form.ref_user)) eq 0)>
  <cfset ArrayAppend(Err.Messages,"You said someone referred you, who was it?">
</cfif>