Question

I have been using ColdFusion 8 / 9 / 10 regularly. The code below works just great in CF9 and CF10. (I developed it in 9). It does NOT work in CF8 though.

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

<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'll get this error:

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

In CF8, how would I modify 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>
Était-ce utile?

La solution

In CFSCRIPT, named arguments are required unless provided with a default (which can't be done until CF9).

To do 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 body of the function. You can do this by taking advantage of ColdFusion's handling of ordinal (ordered instead of 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;
}

Autres conseils

Sean is correct:

Names of the arguments required by the function. The number of arguments passed into the function must equal or exceed the number of arguments in the parentheses at the start of the function definition. If the calling page omits any of the required arguments, ColdFusion generates a mismatched argument count error.

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

I guess you can rewrite it in CFML then it'll 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, do not define the selected value in the function declarator. Just check if arguments[3] is defined. Make sure you document what is expected for arguments[3] in the comment.

p.s. don't forget, you need to Make your own LOCAL scope in CF8... i.e. var LOCAL = {}

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top