SelectViewHelper extends AbstractFormFieldViewHelper
This ViewHelper generates a :html:`<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'}" />
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" />
Generates a dropdown box like above, except that "VISA Card" is selected.
If the select box is a multi-select box :html: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
:html:<option>
and :html:<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>
.. note::
Do not use vanilla :html:<option>
or :html:<optgroup>
tags!
They will invalidate the HMAC generation!
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 :php:__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" />
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 :php:$user->getId()
is called to
retrieve the key, and :php:$user->getFirstName()
to retrieve the displayed
value of each entry.
The value
property now expects a domain object, and tests for object equivalence.
Table of Contents
Properties
- $configurationManager : ConfigurationManagerInterface
- $persistenceManager : PersistenceManagerInterface
- $respectSubmittedDataValue : bool
- $selectedValue : mixed
- $tagName : string
Methods
- getRespectSubmittedDataValue() : bool
- Getting the current configuration for respectSubmittedDataValue.
- initializeArguments() : mixed
- Initialize arguments.
- injectConfigurationManager() : mixed
- injectPersistenceManager() : mixed
- render() : string
- Render the tag.
- setRespectSubmittedDataValue() : mixed
- Define respectSubmittedDataValue to enable or disable the usage of the submitted values in the viewhelper.
- addAdditionalIdentityPropertiesIfNeeded() : mixed
- Add additional identity properties in case the current property is hierarchical (of the form "bla.blubb").
- convertToPlainValue() : mixed
- Converts an arbitrary value to a plain value
- getLastSubmittedFormData() : mixed
- Get the form data which has last been submitted; only returns valid data in case a property mapping error has occurred. Check with hasMappingErrorOccurred() before!
- getMappingResultsForProperty() : Result
- Get errors for the property and form name of this ViewHelper
- getName() : string
- Get the name of this form element.
- getNameWithoutPrefix() : string
- Get the name of this form element, without prefix.
- getOptions() : array<string|int, mixed>
- Render the option tags.
- getOptionValueScalar() : string
- Get the option value for an object
- getPropertyValue() : mixed
- Get the current property of the object bound to this form.
- getRequest() : Request
- Shortcut for retrieving the request from the controller context
- getSelectedValue() : mixed
- Retrieves the selected value(s)
- getValueAttribute() : mixed
- Returns the current value of this Form ViewHelper and converts it to an identifier string in case it's an object The value is determined as follows: * If property mapping errors occurred and the form is re-displayed, the *last submitted* value is returned * Else the bound property is returned (only in objectAccessor-mode) * As fallback the "value" argument of this ViewHelper is used
- getValueFromSubmittedFormData() : mixed
- If property mapping errors occurred and the form is re-displayed, the *last submitted* value is returned by this method.
- hasMappingErrorOccurred() : bool
- Checks if a property mapping error has occurred in the last request.
- isObjectAccessorMode() : bool
- Internal method which checks if we should evaluate a domain object or just output arguments['name'] and arguments['value']
- isSelected() : bool
- Render the option tags.
- prefixFieldName() : string
- Prefixes / namespaces the given name with the form field prefix
- registerFieldNameForFormTokenGeneration() : mixed
- Register a field name for inclusion in the HMAC / Form Token generation
- renderHiddenFieldForEmptyValue() : string
- Renders a hidden field with the same name as the element, to make sure the empty value is submitted in case nothing is selected. This is needed for checkbox and multiple select fields
- renderHiddenIdentityField() : string
- Renders a hidden form field containing the technical identity of the given object.
- renderOptionTag() : string
- Render one option tag
- renderOptionTags() : string
- Render the option tags.
- renderPrependOptionTag() : string
- Render prepended option tag
- setErrorClassAttribute() : mixed
- Add a CSS class if this ViewHelper has errors
Properties
$configurationManager
protected
ConfigurationManagerInterface
$configurationManager
$persistenceManager
protected
PersistenceManagerInterface
$persistenceManager
$respectSubmittedDataValue
protected
bool
$respectSubmittedDataValue
= false
$selectedValue
protected
mixed
$selectedValue
$tagName
protected
string
$tagName
= 'select'
Methods
getRespectSubmittedDataValue()
Getting the current configuration for respectSubmittedDataValue.
public
getRespectSubmittedDataValue() : bool
Return values
boolinitializeArguments()
Initialize arguments.
public
initializeArguments() : mixed
injectConfigurationManager()
public
injectConfigurationManager(ConfigurationManagerInterface $configurationManager) : mixed
Parameters
- $configurationManager : ConfigurationManagerInterface
injectPersistenceManager()
public
injectPersistenceManager(PersistenceManagerInterface $persistenceManager) : mixed
Parameters
- $persistenceManager : PersistenceManagerInterface
render()
Render the tag.
public
render() : string
Return values
string —rendered tag.
setRespectSubmittedDataValue()
Define respectSubmittedDataValue to enable or disable the usage of the submitted values in the viewhelper.
public
setRespectSubmittedDataValue(bool $respectSubmittedDataValue) : mixed
Parameters
- $respectSubmittedDataValue : bool
addAdditionalIdentityPropertiesIfNeeded()
Add additional identity properties in case the current property is hierarchical (of the form "bla.blubb").
protected
addAdditionalIdentityPropertiesIfNeeded() : mixed
Then, [bla][__identity] has to be generated as well.
convertToPlainValue()
Converts an arbitrary value to a plain value
protected
convertToPlainValue(mixed $value) : mixed
Parameters
- $value : mixed
-
The value to convert
getLastSubmittedFormData()
Get the form data which has last been submitted; only returns valid data in case a property mapping error has occurred. Check with hasMappingErrorOccurred() before!
protected
getLastSubmittedFormData() : mixed
getMappingResultsForProperty()
Get errors for the property and form name of this ViewHelper
protected
getMappingResultsForProperty() : Result
Return values
Result —Array of errors
getName()
Get the name of this form element.
protected
getName() : string
Either returns arguments['name'], or the correct name for Object Access.
In case property is something like bla.blubb (hierarchical), then [bla][blubb] is generated.
Return values
string —Name
getNameWithoutPrefix()
Get the name of this form element, without prefix.
protected
getNameWithoutPrefix() : string
Return values
string —name
getOptions()
Render the option tags.
protected
getOptions() : array<string|int, mixed>
Return values
array<string|int, mixed> —an associative array of options, key will be the value of the option tag
getOptionValueScalar()
Get the option value for an object
protected
getOptionValueScalar(mixed $valueElement) : string
Parameters
- $valueElement : mixed
Return values
stringgetPropertyValue()
Get the current property of the object bound to this form.
protected
getPropertyValue() : mixed
Return values
mixed —Value
getRequest()
Shortcut for retrieving the request from the controller context
protected
getRequest() : Request
Return values
RequestgetSelectedValue()
Retrieves the selected value(s)
protected
getSelectedValue() : mixed
Return values
mixed —value string or an array of strings
getValueAttribute()
Returns the current value of this Form ViewHelper and converts it to an identifier string in case it's an object The value is determined as follows: * If property mapping errors occurred and the form is re-displayed, the *last submitted* value is returned * Else the bound property is returned (only in objectAccessor-mode) * As fallback the "value" argument of this ViewHelper is used
protected
getValueAttribute() : mixed
Note: This method should not be used for form elements that must not change the value attribute, e.g. (radio) buttons and checkboxes.
Return values
mixed —Value
getValueFromSubmittedFormData()
If property mapping errors occurred and the form is re-displayed, the *last submitted* value is returned by this method.
protected
getValueFromSubmittedFormData(mixed $value) : mixed
Note: This method should not be used for form elements that must not change the value attribute, e.g. (radio) buttons and checkboxes. The default behaviour is not to use this method. You need to set respectSubmittedDataValue to TRUE to enable the form data handling for the viewhelper.
Parameters
- $value : mixed
Return values
mixed —Value
hasMappingErrorOccurred()
Checks if a property mapping error has occurred in the last request.
protected
hasMappingErrorOccurred() : bool
Return values
bool —TRUE if a mapping error occurred, FALSE otherwise
isObjectAccessorMode()
Internal method which checks if we should evaluate a domain object or just output arguments['name'] and arguments['value']
protected
isObjectAccessorMode() : bool
Return values
bool —TRUE if we should evaluate the domain object, FALSE otherwise.
isSelected()
Render the option tags.
protected
isSelected(mixed $value) : bool
Parameters
- $value : mixed
-
Value to check for
Return values
bool —TRUE if the value should be marked a s selected; FALSE otherwise
prefixFieldName()
Prefixes / namespaces the given name with the form field prefix
protected
prefixFieldName(string $fieldName) : string
Parameters
- $fieldName : string
-
field name to be prefixed
Return values
string —namespaced field name
registerFieldNameForFormTokenGeneration()
Register a field name for inclusion in the HMAC / Form Token generation
protected
registerFieldNameForFormTokenGeneration(string $fieldName) : mixed
Parameters
- $fieldName : string
-
name of the field to register
renderHiddenFieldForEmptyValue()
Renders a hidden field with the same name as the element, to make sure the empty value is submitted in case nothing is selected. This is needed for checkbox and multiple select fields
protected
renderHiddenFieldForEmptyValue() : string
Return values
string —the hidden field.
renderHiddenIdentityField()
Renders a hidden form field containing the technical identity of the given object.
protected
renderHiddenIdentityField(object $object, string $name) : string
Parameters
- $object : object
-
Object to create the identity field for
- $name : string
-
Name
Tags
Return values
string —A hidden field containing the Identity (UID in TYPO3 Flow, uid in Extbase) of the given object or NULL if the object is unknown to the persistence framework
renderOptionTag()
Render one option tag
protected
renderOptionTag(string $value, string $label, bool $isSelected) : string
Parameters
- $value : string
-
value attribute of the option tag (will be escaped)
- $label : string
-
content of the option tag (will be escaped)
- $isSelected : bool
-
specifies whether or not to add selected attribute
Return values
string —the rendered option tag
renderOptionTags()
Render the option tags.
protected
renderOptionTags(array<string|int, mixed> $options) : string
Parameters
- $options : array<string|int, mixed>
-
the options for the form.
Return values
string —rendered tags.
renderPrependOptionTag()
Render prepended option tag
protected
renderPrependOptionTag() : string
Return values
string —rendered prepended empty option
setErrorClassAttribute()
Add a CSS class if this ViewHelper has errors
protected
setErrorClassAttribute() : mixed