SelectViewHelper
class SelectViewHelper extends AbstractFormFieldViewHelper (View source)
This ViewHelper generates a
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 array value is used as human-readable name.
To pre-select a value, set "value" to the option key which should be selected. If the select box is a multi-select box (multiple="true"), then "value" can be an array as well.
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 (UUID/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 specified - the value of the prependOptionValue variable as value.
In the example below, the userArray is an array of "User" domain objects, with no array key specified. Thus 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.
Translation of select content
The ViewHelper can be given a "translate" argument with configuration on how to translate option labels. The array can have the following keys:
- "by" defines if translation by message id or original label is to be used ("id" or "label")
- "using" defines if the option tag's "value" or "label" should be used as translation input, defaults to "value"
- "locale" defines the locale identifier to use, optional, defaults to current locale
- "source" defines the translation source name, optional, defaults to "Main"
- "package" defines the package key of the translation source, optional, defaults to current package
- "prefix" defines a prefix to use for the message id – only works in combination with "by id"
= Examples =
<output>
<select name="paymentOptions">
<option value="payPal">PayPal International Services</option>
<option value="visa">VISA Card</option>
</select>
</output>
<code title="Preselect a default value">
<f:form.select name="paymentOptions" options="{payPal: 'PayPal International Services', visa: 'VISA Card'}" value="visa" />
<output>
(Generates a dropdown box, using ids and first names of the User instances.)
</output>
<code title="Prepend a fixed option">
<f:form.select property="salutation" options="{salutations}" prependOptionLabel="- select one -" />
<output>
(Generates a dropdown box and uses the values "payPal" and "visa" to look up
translations for those ids in the current package's "Main" XLIFF file.)
</output>
<code title="Label translation usign a prefix">
<f:form.select name="paymentOption" options="{payPal: 'PayPal International Services', visa: 'VISA Card'}" translate="{by: 'id', prefix: 'shop.paymentOptions.'}" />
Properties
protected ControllerContext | $controllerContext | Controller Context to use |
from AbstractViewHelper |
protected ObjectManagerInterface | $objectManager | from AbstractViewHelper | |
protected LoggerInterface | $logger | from AbstractViewHelper | |
protected bool | $escapeOutput | Disable escaping of tag based ViewHelpers so that the rendered tag is not htmlspecialchar'd |
from AbstractTagBasedViewHelper |
protected TagBuilder | $tag | Tag builder instance |
from AbstractTagBasedViewHelper |
protected string | $tagName | ||
protected PersistenceManagerInterface | $persistenceManager | from AbstractFormViewHelper | |
protected Translator | $translator | ||
protected mixed | $selectedValue |
Methods
No description
Injects the (system) logger based on PSR-3.
Register a new tag attribute. Tag attributes are all arguments which will be directly appended to a tag if you call $this->initializeTag()
Registers all standard HTML universal attributes.
Handles additional arguments, sorting out any data- prefixed tag attributes and assigning them. Then passes the unassigned arguments to the parent class' method, which in the default implementation will throw an error about "undeclared argument used".
Injects the Flow Persistence Manager
Prefixes / namespaces the given name with the form field prefix
Renders a hidden form field containing the technical identity of the given object.
Register a field name for inclusion in the HMAC / Form Token generation
Initialize arguments.
Shortcut for retrieving the request from the controller context
Get the name of this form element, without prefix.
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
Checks if a property mapping error has occurred in the last request.
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!
Add additional identity properties in case the current property is hierarchical (of the form "bla.blubb").
Get the current property of the object bound to this form.
Returns the "absolute" property path of the property bound to this ViewHelper.
Internal method which checks if we should evaluate a domain object or just output arguments['name'] and arguments['value']
Add an CSS class if this view helper has errors
Get errors for the property and form name of this view helper
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
Render the tag.
Render the option tags.
Render the option tags.
Render the option tags.
Retrieves the selected value(s)
Get the option value for an object
Render one option tag
Returns a translated version of the given label
Details
void
setRenderingContext(RenderingContextInterface $renderingContext)
No description
void
injectObjectManager(ObjectManagerInterface $objectManager)
No description
void
injectLogger(LoggerInterface $logger)
Injects the (system) logger based on PSR-3.
bool
isEscapingInterceptorEnabled()
No description
__construct()
Constructor
void
injectTagBuilder(TagBuilder $tag)
No description
void
initialize()
Sets the tag name to $this->tagName.
Additionally, sets all tag attributes which were registered in $this->tagAttributes and additionalArguments.
Will be invoked just before the render method.
protected void
registerTagAttribute(string $name, string $type, string $description, bool $required = false, mixed $defaultValue = null)
Register a new tag attribute. Tag attributes are all arguments which will be directly appended to a tag if you call $this->initializeTag()
protected void
registerUniversalTagAttributes()
Registers all standard HTML universal attributes.
Should be used inside registerArguments();
void
handleAdditionalArguments(array $arguments)
Handles additional arguments, sorting out any data- prefixed tag attributes and assigning them. Then passes the unassigned arguments to the parent class' method, which in the default implementation will throw an error about "undeclared argument used".
void
injectPersistenceManager(PersistenceManagerInterface $persistenceManager)
Injects the Flow Persistence Manager
protected string
prefixFieldName(string $fieldName)
Prefixes / namespaces the given name with the form field prefix
protected string
renderHiddenIdentityField(object $object, string $name)
Renders a hidden form field containing the technical identity of the given object.
protected void
registerFieldNameForFormTokenGeneration(string $fieldName)
Register a field name for inclusion in the HMAC / Form Token generation
void
initializeArguments()
Initialize arguments.
protected string
getName()
Get the name of this form element.
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.
protected ActionRequest
getRequest()
Shortcut for retrieving the request from the controller context
protected string
getNameWithoutPrefix()
Get the name of this form element, without prefix.
protected mixed
getValueAttribute(bool $ignoreSubmittedFormData = false)
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 bool
hasMappingErrorOccurred()
Checks if a property mapping error has occurred in the last request.
protected mixed
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 void
addAdditionalIdentityPropertiesIfNeeded()
Add additional identity properties in case the current property is hierarchical (of the form "bla.blubb").
Then, [bla][__identity] has to be generated as well.
protected mixed
getPropertyValue()
Get the current property of the object bound to this form.
protected string
getPropertyPath()
Returns the "absolute" property path of the property bound to this ViewHelper.
For
protected bool
isObjectAccessorMode()
Internal method which checks if we should evaluate a domain object or just output arguments['name'] and arguments['value']
protected void
setErrorClassAttribute()
Add an CSS class if this view helper has errors
protected Result
getMappingResultsForProperty()
Get errors for the property and form name of this view helper
protected void
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
string
render()
Render the tag.
protected string
renderOptionTags(array $options)
Render the option tags.
protected array
getOptions()
Render the option tags.
protected bool
isSelected(mixed $value)
Render the option tags.
protected mixed
getSelectedValue()
Retrieves the selected value(s)
protected string
getOptionValueScalar(mixed $valueElement)
Get the option value for an object
protected string
renderOptionTag(string $value, string $label)
Render one option tag
protected string
getTranslatedLabel(string $value, string $label)
Returns a translated version of the given label