需要进行/或cfargument

时间:2014-09-08 20:56:15

标签: coldfusion coldfusion-10

产品插入页面上有两个表单元素,可以指定产品的制造商。一个是下拉列表,另一个是文本字段。

当用户想要提交产品以便插入数据库时​​,他必须提供产品的制造商。因此,要么从下拉列表中选择它,要么在文本字段中输入制造商的名称。客户端JavaScript验证可以确保至少填写一个。

然而,在我负责插入的CFC中,我如何确保其中至少有一个字段具有值?伪代码风格我正在思考:

<cfargument name="ManufacturerID" type="numeric" required="true"/> // drop-down value
OR 
<cfargument name="Manufacturer" type="string" required="true"/>    // textfield value

基本上我的问题是如何在服务器端验证表单值,其中所需状态依赖于现有或不存在的另一个表单元素的值?

3 个答案:

答案 0 :(得分:2)

这是Kevin B的评论的变体。正如他所说,使两个参数都是可选的。但是,请将您的验证作为表单处理页面的一部分,而不是cfc。

 <cfif len(form.ManufacturerId) gt 0 
 or len(form.Manufacturer) gt 0>
 call your cfc
 <cfelse>
 display something to user.
 </cfif>

答案 1 :(得分:1)

我会在我的组件中做这样的事情:

<cffunction name="insertProduct" ...>
  ...
  <cfargument name="ManufacturerID" required="false" type="numeric">
  <cfargument name="Manufacturer required="false" type="string">
  ...
  <cfif NOT (
    (StructKeyExists(arguments,"ManufacturerID") AND isNumeric(arguments.ManufacturerID))
    OR 
    (StructKeyExists(arguments,"Manufacturer") AND len(trim(arguments.Manufacturer)) GT 0)
  )>
    <cfthrow type="ValidationError" message="Either 'ManufacturerID' or 'Manufacturer' must be supplied to the insertProduct function">
  </cfif>
  ...

答案 2 :(得分:1)

我建议您在UI和cfc中添加验证。

首先,我建议使用方法级别验证,以确保从任何应用程序调用调用时您的函数正常工作。

如果你想让它们可以访问帮助/提示,​​java文档等,你必须在你的CFC中声明这两个参数,但如果它们是可选的,你没有理由需要它们。

您也可以拥有未声明的重载参数,并强制执行其中一个或另一个在函数中发送,类似于Ice Below the Ice建议的内容。

对于声明的参数,没有必要,没有理由用structKeyExists()检查,因为记录的参数键将始终存在。检查未记录的重载参数只会很有用。

使用CF9 + cfscript样式(如果需要,您可以在标签中同样执行此操作):

public function insertProduct(numeric ManufacturerId , string ManufacturerName ) {
  if(!isDefined('Arguments.MfgId') && !isDefined('Arguments.MfgName') 
      throw("You must provide either the ManufacturerId or the ManufacturerName argument", "Validation");

  // Continue with your code
  return 'Your Result';

}

其次,在UI级别预先验证

是一种很好的做法
  • 立即允许友好的用户界面反馈。
  • 减少对您已知的服务器的调用将失败。

用于UI。我会使用JS来做这件事,而不是等待提交表单。

<form id='ManForm' action='somewhere_else' method='post'>
  <label>Select A Manufacturer:</label>
  <select name='ManufacturerId' id='ManufacturerId'>
    <option value='0'>-- Select --</option>
    <option value='1'>1: JohnnyDear</option>
    <option value='2'>2: Cattypillar</option>
  </select>
  <label>Search By Name:</label>
  <input type='text' name='ManufacturerName' id='ManufacturerName'/>
  <button id='submitForm'>Submit</button>
 </form>

和js(包含jQuery库):

$(function() {
  // BIND ON CLICK TO BUTTON
  $('#submitForm').click(function(event) {

      // VALIDATE
      var _mfrId = $('#ManufacturerId');
      var _mfrNm = $('#ManufacturerName');
      if(_mfrId.val() == 0 && _mfrNm.val().length < 1) {
          // Inform User of issue
          alert('You must select a Manufacturer by Id or\n enter a Name before submitting');
          return false;
      }

      // SUBMIT - If validation did not return false, the form will submit
      $('#submitForm').submit();
  });
})

您可以进一步封装和更改javascript以执行更多有趣的操作,但这将帮助您入门。我已经添加了一个jsfiddle链接来显示此UI代码:http://jsfiddle.net/n12ugzwo/2/

最后,您的表单处理程序需要进行一些基本验证并映射到您的cfc调用。 如果用户击败了js验证,您仍然希望您的处理程序进行检查。如果两者都进来,您也可能想要确定使用WHICH值。您还可以添加一些UI / js验证/帮助以让用户也知道这一点。

在表单处理程序(操作)页面中:

<cfscript>
  if(isDefined('Form.ManufacturerId')) {
    if(len(Form.ManufacturerName)) { // Any value gt 0 will return true
      Variables.ProductInsertResult = insertProduct(ManufacturerName: Form.ManufacturerName);
    } else if(Form.ManufacturerId) { // Any value gt 0 will return true
      Variables.ProductInsertResult = insertProduct(ManufacturerId: Form.ManufacturerId);
    } else {
      writeoutput('You must select a Manufacturer by Id or enter a Name before submitting.<br>');
      writeoutput('Please go back and resubmit with valid entries.');
    }
  }
</cfsript>