How do I make ARGUMENTS optional in ColdFusion 8?

I use ColdFusion 8/9/10 regularly. The code below works great in CF9 and CF10. (I designed it at 9). However, it does NOT work in CF8.

If you run the code below (bottom) in CF9 and CF10, you should immediately get HTML results:

<select>
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
    <option selected="" value="3">Option 3</option>
</select>

      

If you run the code below in CF8 you will get this error:

The SELECTED parameter to the WrapOption function is required but was not passed in.

      

In CF8, how do I change this code to make the "selected" parameter (or any other parameter) optional in CF8?

<cfscript>

Options = WrapOption("Option 1", 1);
Options = Options & WrapOption("Option 2", 2);
Options = Options & WrapOption("Option 3", 3, "Selected");
SelectBox = WrapSelect(Options);
writeOutput(SelectBox);

// WRAP OPTION
function WrapOption(Content, Value, Selected) {
    LOCAL.Content = ARGUMENTS.Content;
    LOCAL.Properties = " value='#ARGUMENTS.Value#'";
    // SELECTED
    if (structKeyExists(ARGUMENTS, "Selected")) {
        LOCAL.Properties = LOCAL.Properties & " selected";
    }
    LOCAL.Item = "<option #LOCAL.Properties#>#LOCAL.Content#</option>";
    return LOCAL.Item;
}
// WRAP SELECT
function WrapSelect(Options, Class, ID) {
    LOCAL.Options = ARGUMENTS.Options;
    LOCAL.Properties = "";
    // CLASS
    if (structKeyExists(ARGUMENTS, "Class")) {
        LOCAL.Properties = LOCAL.Properties & " class='#ARGUMENTS.Class#'";
    }
    // ID
    if (structKeyExists(ARGUMENTS, "ID")) {
        LOCAL.Properties = LOCAL.Properties & " id='#ARGUMENTS.ID#'";
    }
    LOCAL.Item = "<select #LOCAL.Properties#>#LOCAL.Options#</select>";
    return LOCAL.Item;
}
</cfscript>

      

+3


source to share


2 answers


In CFSCRIPT, named arguments are required unless they are provided by default (which could not be done prior to CF9).

To make optional arguments in CFSCRIPT in ColdFusion 8 and below, you need to remove the argument from the function definition and check for its existence in the function body. You can do this by taking advantage of ColdFusion's handling of ordinal (ordered, not named) arguments.



function WrapOption(Content, Value) {
    if ( ArrayLen(Arguments) GTE 3 ) {
        ARGUMENTS.Selected = ARGUMENTS[3];
    }   
    LOCAL.Content = ARGUMENTS.Content;
    LOCAL.Properties = " value='#ARGUMENTS.Value#'";
    // SELECTED
    if (structKeyExists(ARGUMENTS, "Selected")) {
        LOCAL.Properties = LOCAL.Properties & " selected";
    }
    LOCAL.Item = "<option #LOCAL.Properties#>#LOCAL.Content#</option>";
    return LOCAL.Item;
}

      

+7


source


Sean is right:

The names of the arguments required by the function. The number of arguments passed to the function must be equal to or greater than the number of arguments in parentheses at the beginning of the function definition. If the calling page omits any of the required arguments, ColdFusion generates a mismatched argument error.

Quoted from: http://livedocs.adobe.com/coldfusion/8/htmldocs/UDFs_03.html

I think you can rewrite it in CFML, then it will work for sure.



// WRAP OPTION
<cffunction name="WrapOption" output="false">
  <cfargument name="Content" required="true">
  <cfargument name="Value" required="true">
  <cfargument name="Selected">
  <cfscript>
    LOCAL.Content = ARGUMENTS.Content;
    LOCAL.Properties = " value='#ARGUMENTS.Value#'";
    // SELECTED
    if (structKeyExists(ARGUMENTS, "Selected")) {
        LOCAL.Properties = LOCAL.Properties & " selected";
    }
    LOCAL.Item = "<option #LOCAL.Properties#>#LOCAL.Content#</option>";
    return LOCAL.Item;
  <cfscript>
</cffunction>

      

Or, alternatively, as a workaround for CF8, don't define the selected value in the function declarator. Just check if it exists arguments[3]

. Make sure you document what is expected for arguments [3] in a comment.

ps don't forget you need to make your own LOCAL range in CF8 ... i.e. var LOCAL = {}

+3


source







All Articles