Skip to content

View Helpers

hasManyCheckBox()

hasManyCheckBox() — returns string

Available in: controller Category: Form Association Functions

Used as a shortcut to output the proper form elements for an association. Note: Pass any additional arguments like class, rel, and id, and the generated tag will also include those values as HTML attributes.

NameTypeRequiredDefaultDescription
objectNamestringyes—Name of the variable containing the parent object to represent with this form field.
associationstringyes—Name of the association set in the parent object to represent with this form field.
keysstringyes—Keys of the join row for this form field: the parent key, then the other key (for example a post key and a tag id). For a composite-key join model, list them in the order of its primary key columns. A surrogate-id join model works too: the keys are matched on its belongsTo foreign keys. Either way, declare a belongsTo association on the join model for each key column.
labelstringno—The label text to use in the form control.
labelPlacementstringno—Whether to place the label before, after, or wrapped around the form control. Label text placement can be controlled using aroundLeft or aroundRight.
prependstringno—String to prepend to the form control. Useful to wrap the form control with HTML tags.
appendstringno—String to append to the form control. Useful to wrap the form control with HTML tags.
prependToLabelstringno—String to prepend to the form control’s label. Useful to wrap the form control with HTML tags.
appendToLabelstringno—String to append to the form control’s label. Useful to wrap the form control with HTML tags.
errorElementstringno—HTML tag to wrap the form control with when the object contains errors.
errorClassstringno—The class name of the HTML tag that wraps the form control when there are errors.
encodeanynotrueUse this argument to decide whether the output of the function should be encoded in order to prevent Cross Site Scripting (XSS) attacks. Set it to true to encode all relevant output for the specific HTML element in question (e.g. tag content, attribute values, and URLs). For HTML elements that have both tag content and attribute values you can set this argument to attributes to only encode attribute values and not tag content.
idstringno—Optional. Explicit ID for the generated checkbox input. If not provided, an ID will be generated automatically.
// 1. Show check boxes for associating authors with the current book
<cfloop query="authors">
    #hasManyCheckBox(
        label=authors.fullName,
        objectName="book",
        association="bookAuthors",
        keys="#book.key()#,#authors.id#"
    )#
</cfloop>

// 2. Wrap each checkbox and label in a div using prepend/append
<cfloop query="authors">
    #hasManyCheckBox(
        label=authors.fullName,
        labelPlacement="after",
        objectName="book",
        association="bookAuthors",
        keys="#book.key()#,#authors.id#",
        prepend="<div class=""author-option"">",
        append="</div>"
    )#
</cfloop>

// 3. Supply an explicit ID and custom error styling
<cfloop query="tags">
    #hasManyCheckBox(
        label=tags.name,
        objectName="post",
        association="postTags",
        keys="#post.key()#,#tags.id#",
        id="tag-#tags.id#",
        errorElement="span",
        errorClass="field-error"
    )#
</cfloop>