Back

select()

Builds and returns an HTML <select> element bound to a model object property. It automatically handles nested associations, labels, options, and error highlighting. You can provide a list of options as a query, array of objects, or simple array, and customize labels, HTML attributes, and encoding. It is especially useful for forms where a user must select a value from a predefined list that is related to a database model. Note: Pass any additional arguments like class, rel, and id, and the generated tag will also include those values as HTML attributes.

Name Type Required Default Description
objectName any Yes The variable name of the object to build the form control for.
property string Yes The name of the property to use in the form control.
association string No The name of the association that the property is located on. Used for building nested forms that work with nested properties. If you are building a form with deep nesting, simply pass in a list to the nested object, and Wheels will figure it out.
position string No The position used when referencing a hasMany relationship in the association argument. Used for building nested forms that work with nested properties. If you are building a form with deep nestings, simply pass in a list of positions, and Wheels will figure it out.
options any No A collection to populate the select form control with. Can be a query recordset or an array of objects.
includeBlank any No false Whether to include a blank option in the select form control. Pass true to include a blank line or a string that should represent what display text should appear for the empty value (for example, "- Select One -").
valueField string No The column or property to use for the value of each list element. Used only when a query or array of objects has been supplied in the options argument. Required when specifying textField
textField string No The column or property to use for the value of each list element that the end user will see. Used only when a query or array of objects has been supplied in the options argument. Required when specifying valueField
label string No useDefaultLabel The label text to use in the form control.
labelPlacement string No around Whether to place the label before, after, or wrapped around the form control. Label text placement can be controlled using aroundLeft or aroundRight.
prepend string No String to prepend to the form control. Useful to wrap the form control with HTML tags.
append string No String to append to the form control. Useful to wrap the form control with HTML tags.
prependToLabel string No String to prepend to the form control's label. Useful to wrap the form control with HTML tags.
appendToLabel string No String to append to the form control's label. Useful to wrap the form control with HTML tags.
errorElement string No span HTML tag to wrap the form control with when the object contains errors.
errorClass string No field-with-errors The class name of the HTML tag that wraps the form control when there are errors.
encode any No true Use 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 select bound to a model property
authors = model("author").findAll();

<!--- View code --->
#select(objectName="book", property="authorId", options=authors)#

2. Using valueField and textField
select(
    objectName="book",
    property="authorId",
    options=authors,
    valueField="id",
    textField="authorfullname"
)

3. Include blank option
select(
    objectName="book",
    property="authorId",
    options=authors,
    valueField="id",
    textField="authorfullname",
    includeBlank="- Select Author -"
)

4. Nested hasMany association
<cfloop from="1" to="#ArrayLen(shipments.orders)#" index="i">
    select(
        label="Order #shipments.orders[i].orderNum#",
        objectName="shipment",
        association="orders",
        position=i,
        property="statusId",
        options=statuses,
        valueField="id",
        textField="name"
    )
</cfloop>

5. Custom label and placement
select(
    objectName="book",
    property="authorId",
    options=authors,
    valueField="id",
    textField="authorfullname",
    label="Choose Author",
    labelPlacement="before"
)
Copied!