Skip to content

View Helpers

hasManyRadioButton()

hasManyRadioButton() — returns string

Available in: controller Category: Form Association Functions

This helper generates radio buttons for managing a hasMany or one-to-many association, where you want the user to pick one option (e.g., default address, primary contact method, preferred category). Note: Pass any additional arguments like class, rel, and id, and the generated tag will also include those values as HTML attributes.

NameTypeRequiredDefaultDescription
objectNamestringyesName of the variable containing the parent object to represent with this form field.
associationstringyesName of the association set in the parent object to represent with this form field.
propertystringyesName of the property in the child object to represent with this form field.
keysstringyesPrimary keys associated with this form field. Note that these keys should be listed in the order that they appear in the database table.
tagValuestringyesThe value of the radio button when selected.
checkIfBlankbooleannofalseWhether or not to check this form field as a default if there is a blank value set for the property.
labelstringnoThe label text to use in the form control.
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.
1. Basic usage (Author -> Default Address)

// Pick one address as the author’s default
<cfloop query="addresses">
    #hasManyRadioButton(
        objectName="author",
        association="authorsDefaultAddresses",
        property="defaultAddressId",
        keys="#author.key()#,#addresses.id#",
        tagValue="#addresses.id#",
        label=addresses.title
    )#
</cfloop>

2. Pre-check default radio if property is blank

// If no address is selected yet, pre-check the "Home" option
<cfloop query="addresses">
    #hasManyRadioButton(
        objectName="author",
        association="authorsDefaultAddresses",
        property="defaultAddressId",
        keys="#author.key()#,#addresses.id#",
        tagValue="#addresses.id#",
        label=addresses.title,
        checkIfBlank=(addresses.title EQ "Home")
    )#
</cfloop>

3. Style with extra HTML attributes

// Add class and id for custom styling
<cfloop query="paymentMethods">
    #hasManyRadioButton(
        objectName="user",
        association="userPaymentMethods",
        property="defaultPaymentMethodId",
        keys="#user.key()#,#paymentMethods.id#",
        tagValue="#paymentMethods.id#",
        label=paymentMethods.name,
        class="radio-option",
        id="paymentMethod_#paymentMethods.id#"
    )#
</cfloop>