Form.select ViewHelper <f:form.select>

This ViewHelper generates a <select> dropdown list for the use with a form.

Basic usage

The most straightforward way is to supply an associative array as the options parameter. The array key is used as option key, and the value is used as human-readable name.

Basic usage:

<f:form.select name="paymentOptions" options="{payPal: 'PayPal International Services', visa: 'VISA Card'}" />
Copied!

Pre select a value

To pre select a value, set value to the option key which should be selected. Default value:

<f:form.select name="paymentOptions" options="{payPal: 'PayPal International Services', visa: 'VISA Card'}" value="visa" />
Copied!

Generates a dropdown box like above, except that "VISA Card" is selected.

If the select box is a multi-select box multiple="1", then "value" can be an array as well.

Custom options and option group rendering

Child nodes can be used to create a completely custom set of <option> and <optgroup> tags in a way compatible with the HMAC generation. To do so, leave out the options argument and use child ViewHelpers:

Custom options and optgroup:

<f:form.select name="myproperty">
   <f:form.select.option value="1">Option one</f:form.select.option>
   <f:form.select.option value="2">Option two</f:form.select.option>
   <f:form.select.optgroup>
      <f:form.select.option value="3">Grouped option one</f:form.select.option>
      <f:form.select.option value="4">Grouped option twi</f:form.select.option>
   </f:form.select.optgroup>
</f:form.select>
Copied!

Usage on domain objects

If you want to output domain objects, you can just pass them as array into the options parameter. To define what domain object value should be used as option key, use the optionValueField variable. Same goes for optionLabelField. If neither is given, the Identifier (UID/uid) and the __toString() method are tried as fallbacks.

If the optionValueField variable is set, the getter named after that value is used to retrieve the option key. If the optionLabelField variable is set, the getter named after that value is used to retrieve the option value.

If the prependOptionLabel variable is set, an option item is added in first position, bearing an empty string or - if provided, the value of the prependOptionValue variable as value.

Domain objects:

<f:form.select name="users" options="{userArray}" optionValueField="id" optionLabelField="firstName" />
Copied!

In the above example, the userArray is an array of "User" domain objects, with no array key specified.

So, in the above example, the method $user->getId() is called to retrieve the key, and $user->getFirstName() to retrieve the displayed value of each entry.

The value property now expects a domain object, and tests for object equivalence.

Source code

Go to the source code of this ViewHelper: Form\SelectViewHelper.php (GitHub).

Arguments

The following arguments are available for the form.select ViewHelper:

additionalAttributes

additionalAttributes
Type
array
Additional tag attributes. They will be added directly to the resulting HTML tag.

aria

aria
Type
array
Additional aria-* attributes. They will each be added with a "aria-" prefix.

data

data
Type
array
Additional data-* attributes. They will each be added with a "data-" prefix.

errorClass

errorClass
Type
string
Default
'f3-form-error'
CSS class to set if there are errors for this ViewHelper

multiple

multiple
Type
boolean
Default
false
If set multiple options may be selected.

name

name
Type
string
Name of input tag

optionLabelField

optionLabelField
Type
string
If specified, will call the appropriate getter on each object to determine the label.

optionValueField

optionValueField
Type
string
If specified, will call the appropriate getter on each object to determine the value.

options

options
Type
array
Associative array with internal IDs as key, and the values are displayed in the select box. Can be combined with or replaced by child f:form.select.* nodes.

optionsAfterContent

optionsAfterContent
Type
boolean
Default
false
If true, places auto-generated option tags after those rendered in the tag content. If false, automatic options come first.

prependOptionLabel

prependOptionLabel
Type
string
If specified, will provide an option at first position with the specified label.

prependOptionValue

prependOptionValue
Type
string
If specified, will provide an option at first position with the specified value.

property

property
Type
string
Name of Object Property. If used in conjunction with <f:form object="...">, the "name" property will be ignored, while "value" can be used to specify a default field value instead of the object property value.

required

required
Type
boolean
Default
false
If set no empty value is allowed.

selectAllByDefault

selectAllByDefault
Type
boolean
Default
false
If specified options are selected if none was set before.

sortByOptionLabel

sortByOptionLabel
Type
boolean
Default
false
If true, List will be sorted by label.

value

value
Type
mixed
Value of input tag